1

我有点卡在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,但不知何故我必须将其更改为布尔值,因为这是我的切换按钮的状态。

我知道我必须使用 .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的开发者网站,但这让我很头疼)

4

1 回答 1

0

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

为了处理 SQLite 上的布尔列,我通常将这些数据处理为带有值的 INTEGER10

   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;
   }
于 2013-11-15T23:27:06.713 回答