1

I'm a little bit stuck on viewbinders in android

here's my code:

    public void displayAllAlerts() {
    Cursor mCursor = mDbAdapter.fetchAllAlerts();


    //Bind Columns
    String[] columns = new String[] {
        DbAdapter.KEY_ID,
        DbAdapter.KEY_PLACE,
        DbAdapter.KEY_LONG,
        DbAdapter.KEY_LAT,
        DbAdapter.KEY_STATUS
    };

    int[] to = new int[] {
            R.id.txtId,
            R.id.txtPlace,
            R.id.txtLong,
            R.id.txtLat,
            R.id.tglBtnAlert
    };

    mSimpleCursorAdapter = new SimpleCursorAdapter(
            this,
            R.layout.layout_lvrow,
            mCursor,
            columns,
            to,
            0);



    ListView lvAlerts = (ListView) findViewById(R.id.lvAlerts);
    lvAlerts.setAdapter(mSimpleCursorAdapter);


}

The problem is that 'DbAdapter.key_status' is formatted as an int in my database, but someway I have to change it to a boolean, beacuase it's my status for my togglebutton.

I know i have to use .setViewBinder, but i have no idea were to start.

I tried the following from some tutorials but it does not work:

    mSimplecursorAdapter.setViewBinder(new ViewBinder() {

       public boolean setViewValue(View aView, Cursor aCursor, int aColumnIndex) {

            if (aColumnIndex == 5) {
                String strBool = aCursor.getString(aColumnIndex);
                ToggleButton tb = (Togglebutton) aView;
                if (strBool=="0") {
                    tb.setChecked = false;
                }else{
                    tb.setChecked = true;
                }
            return true;
     }

     return false;
}

thanks in advance

(also tried already to use developer site of android but it's giving me a real headache)

NVandyck
  • 13
  • 3

1 Answers1

0

The code does not work because you must use String.equals() or TextUtils.equals() to compare strings.

To handle boolean columns on SQLite, I usually handle this data as INTEGER with values 1 or 0:

   public boolean setViewValue(View aView, Cursor aCursor, int aColumnIndex) {

        if (aColumnIndex == 5) {
            boolean checked = aCursor.getInt(aColumnIndex) == 1;
            ToggleButton tb = (Togglebutton) aView;
            tb.setChecked(checked);
            return true;
        }
        return false;
   }
Bismark Ito
  • 651
  • 6
  • 8
  • ok this works a little bit more (only change 'return true' to 'return false'. the problem I still have is, it won't change my togglebutton-tekst, it still shows 1/0 instead of on/off the first time. when I click the button, then it changes like it is supposed to. – NVandyck Nov 16 '13 at 19:46
  • Regarding the text displayed on ToggleButton, you can set the displayed values at xml (via `android:textOff` and `android:textOn`), or by code (`ToggleButton.setTextOff()` and `ToggleButton.setTextOn()`). Which one are you using? – Bismark Ito Nov 16 '13 at 23:00