1

我卡在viewbinders在android系統在sqliteDb轉換一個int與.setViewBinder(機器人)

這裏一點點的布爾是我的代碼:

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); 


} 

的問題是,「DbAdapter.key_status '在我的數據庫中被格式化爲一個int,但不管怎樣,我必須將其更改爲布爾值,因爲它是我的togglebutton的狀態。

我知道我必須使用.setViewBinder,但我不知道要開始。

我試圖從一些教程以下,但它不工作:

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; 
} 

在此先感謝

(也嘗試已經使用的Android開發者網站,但它給了我一個真正的頭痛)

回答

0

代碼不起作用,因爲您必須使用String.equals()或TextUtils.equals()來比較字符串。

來處理SQLite的布爾列,我通常處理此數據爲整數,值10

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; 
    } 
+0

確定這工作多一點點(只改變「返回true」到「返回false」。我仍然存在的問題是,它不會改變我的togglebutton-tekst,它仍然會顯示1/0而不是第一次開/關。當我點擊按鈕時,它會像它應該改變的那樣。 – NVandyck

+0

關於顯示在ToggleButton上的文本,你可以在xml(通過'android:textOff'和'android:textOn')或代碼('ToggleButton.setTextOff()'和'ToggleButton.setTextOn()')設置顯示值。你有用嗎? –