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開發者網站,但它給了我一個真正的頭痛)
確定這工作多一點點(只改變「返回true」到「返回false」。我仍然存在的問題是,它不會改變我的togglebutton-tekst,它仍然會顯示1/0而不是第一次開/關。當我點擊按鈕時,它會像它應該改變的那樣。 – NVandyck
關於顯示在ToggleButton上的文本,你可以在xml(通過'android:textOff'和'android:textOn')或代碼('ToggleButton.setTextOff()'和'ToggleButton.setTextOn()')設置顯示值。你有用嗎? –