使用.setViewBinder(android)将sqliteDb中的int转换为布尔值
我有点被android中的活页夹卡住了 这是我的密码:使用.setViewBinder(android)将sqliteDb中的int转换为布尔值,android,sqlite,android-listview,simplecursoradapter,android-viewbinder,Android,Sqlite,Android Listview,Simplecursoradapter,Android Viewbinder,我有点被android中的活页夹卡住了 这是我的密码: public void displayAllAlerts() { Cursor mCursor = mDbAdapter.fetchAllAlerts(); //Bind Columns String[] columns = new String[] { DbAdapter.KEY_ID, DbAdapter.KEY_PLACE, DbAdapter.KEY
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,但我必须以某种方式将其更改为布尔值,因为它是切换按钮的状态
我知道我必须使用.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开发者网站,但这让我非常头疼,因为你必须使用String.equals或TextUtils.equals来比较字符串,所以代码不起作用 为了处理SQLite上的布尔列,我通常将此数据作为值为1或0的整数处理:
好的,这只需要将“return true”更改为“return false”。我仍然存在的问题是,它不会改变我的切换按钮tekst,它仍然显示1/0而不是第一次打开/关闭。当我单击按钮时,它会像预期的那样发生变化。关于ToggleButton上显示的文本,您可以通过android:textOff和android:textOn,或通过代码ToggleButton.setTextOff和ToggleButton.setTextOn,在xml上设置显示值。你用哪一种?
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;
}