使用.setViewBinder(android)将sqliteDb中的int转换为boolean

时间:2013-11-15 20:37:15

标签: android sqlite android-listview simplecursoradapter android-viewbinder

我有点卡在android

中的viewbinders上

这是我的代码:

    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的开发者网站,但它给了我一个真正的头痛)

1 个答案:

答案 0 :(得分:0)

代码不起作用,因为您必须使用String.equals()或TextUtils.equals()来比较字符串。

要在SQLite上处理布尔列,我通常将这些数据作为INTEGER处理,其值为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;
   }