Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用.setViewBinder(android)将sqliteDb中的int转换为布尔值_Android_Sqlite_Android Listview_Simplecursoradapter_Android Viewbinder - Fatal编程技术网

使用.setViewBinder(android)将sqliteDb中的int转换为布尔值

使用.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

我有点被android中的活页夹卡住了

这是我的密码:

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