长时间潜伏-第一篇文章:

问题:当我单击一个图像按钮(基本上只是复选框)时,它们将正确更新我的数据库行并将图像更改为适当的布尔值。但是,当我再次单击它们时-它不会重新更新并发出相同的消息。

我认为问题是:我不是Android的新手,但是我很确定数据库正确更新时,我的bindview变量不是吗?

我的光标适配器BindView:

        @Override
public void bindView(View view, final Context context, final Cursor cursor) {
    final String id = cursor.getString(cursor.getColumnIndex(DBHELPER.WISHLIST_COLUMN_ID));
    final String name2 = cursor.getString(cursor.getColumnIndex(DBHELPER.WISHLIST_COLUMN_NAME));
    final String gift = cursor.getString(cursor.getColumnIndex(DBHELPER.WISHLIST_COLUMN_GIFT));
    final String specs = cursor.getString(cursor.getColumnIndex(DBHELPER.WISHLIST_COLUMN_SPECIFICS));
    final String store = cursor.getString(cursor.getColumnIndex(DBHELPER.WISHLIST_COLUMN_STORE));
    final String url = cursor.getString(cursor.getColumnIndex(DBHELPER.WISHLIST_COLUMN_URL));
    final String status = cursor.getString(cursor.getColumnIndex(DBHELPER.WISHLIST_COLUMN_STATUS));

    ImageButton checkBoxImage = (ImageButton) view.findViewById(R.id.ID_ROW_CHECKBOX);

    if(status.equals("true"))
    {
        checkBoxImage.setImageResource(R.drawable.icon_yes);
    } else {
        checkBoxImage.setImageResource(R.drawable.icon_no);
    }

    checkBoxImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(status.equals("true"))
            {
                ContentValues values = new ContentValues();
                values.put(DBHELPER.WISHLIST_COLUMN_NAME, name2);
                values.put(DBHELPER.WISHLIST_COLUMN_GIFT, gift);
                values.put(DBHELPER.WISHLIST_COLUMN_SPECIFICS, specs);
                values.put(DBHELPER.WISHLIST_COLUMN_STORE, store);
                values.put(DBHELPER.WISHLIST_COLUMN_URL, url);
                values.put(DBHELPER.WISHLIST_COLUMN_STATUS, "false");

                DBHELPER dbhelper = new DBHELPER(context);
                SQLiteDatabase db = dbhelper.getWritableDatabase();

                db.update(DBHELPER.WISHLIST_TABLE_NAME, values,"_ID " + "='" + id + "'", null);
                Message.message(context, "FALSE UPDATED");

                ImageButton checkBoxImage = (ImageButton) v.findViewById(R.id.ID_ROW_CHECKBOX);
                checkBoxImage.setImageResource(R.drawable.icon_no);

                db.close();
            } else {
                DBHELPER dbhelper = new DBHELPER(context);
                SQLiteDatabase db = dbhelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put(DBHELPER.WISHLIST_COLUMN_NAME, name2);
                values.put(DBHELPER.WISHLIST_COLUMN_GIFT, gift);
                values.put(DBHELPER.WISHLIST_COLUMN_SPECIFICS, specs);
                values.put(DBHELPER.WISHLIST_COLUMN_STORE, store);
                values.put(DBHELPER.WISHLIST_COLUMN_URL, url);
                values.put(DBHELPER.WISHLIST_COLUMN_STATUS, "true");

                ImageButton checkBoxImage = (ImageButton) v.findViewById(R.id.ID_ROW_CHECKBOX);
                checkBoxImage.setImageResource(R.drawable.icon_yes);

                System.out.println("BEFORE : " + status);
                db.update(DBHELPER.WISHLIST_TABLE_NAME, values,"_ID " + "='" + id + "'", null);
                 Message.message(context, "TRUE UPDATED");


                db.close();
            }
        }
    });


我尝试过的操作:因此,我单击图像,它将从“已选中”变为“未选中”,反之亦然,但是除非重新加载列表,否则只允许一次。

我已经尝试过:重新查询不断出现,但是现在认为这已经过时了,应该避免。我看到了几篇关于将查询换成新查询的文章,但是我一直收到一个空错误,所以我不确定这是否是答案,我只是误解了什么。

谢谢,谢谢你们。

最佳答案

if内的OnClickListener语句未检查status列的最新值。您需要在OnClickListener中再次阅读它。像这样更改代码:

checkBoxImage.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        DBHELPER dbhelper = new DBHELPER(context);
        SQLiteDatabase db = dbhelper.getWritableDatabase();

        ContentValues values = new ContentValues();

        String[] columns = new String[]{DBHELPER.WISHLIST_COLUMN_NAME, DBHELPER.WISHLIST_COLUMN_GIFT, DBHELPER.WISHLIST_COLUMN_SPECIFICS, DBHELPER.WISHLIST_COLUMN_STORE, DBHELPER.WISHLIST_COLUMN_URL, DBHELPER.WISHLIST_COLUMN_STATUS};
        Cursor cursor2 =  db.query(DBHELPER.WISHLIST_TABLE_NAME, columns, null, null, null, null, null);

        String status2 = cursor2.getString(cursor2.getColumnIndex(DBHELPER.WISHLIST_COLUMN_STATUS));

        if("true".equals(status2)) {
            values.put(DBHELPER.WISHLIST_COLUMN_NAME, name2);
            values.put(DBHELPER.WISHLIST_COLUMN_GIFT, gift);
            values.put(DBHELPER.WISHLIST_COLUMN_SPECIFICS, specs);
            values.put(DBHELPER.WISHLIST_COLUMN_STORE, store);
            values.put(DBHELPER.WISHLIST_COLUMN_URL, url);
            values.put(DBHELPER.WISHLIST_COLUMN_STATUS, "false");

            db.update(DBHELPER.WISHLIST_TABLE_NAME, values,"_ID " + "='" + id + "'", null);
            Message.message(context, "FALSE UPDATED");

            ImageButton checkBoxImage = (ImageButton) v.findViewById(R.id.ID_ROW_CHECKBOX);
            checkBoxImage.setImageResource(R.drawable.icon_no);
        } else {
            values.put(DBHELPER.WISHLIST_COLUMN_NAME, name2);
            values.put(DBHELPER.WISHLIST_COLUMN_GIFT, gift);
            values.put(DBHELPER.WISHLIST_COLUMN_SPECIFICS, specs);
            values.put(DBHELPER.WISHLIST_COLUMN_STORE, store);
            values.put(DBHELPER.WISHLIST_COLUMN_URL, url);
            values.put(DBHELPER.WISHLIST_COLUMN_STATUS, "true");

            ImageButton checkBoxImage = (ImageButton) v.findViewById(R.id.ID_ROW_CHECKBOX);
            checkBoxImage.setImageResource(R.drawable.icon_yes);

            System.out.println("BEFORE : " + status2);
            db.update(DBHELPER.WISHLIST_TABLE_NAME, values,"_ID " + "='" + id + "'", null);
            Message.message(context, "TRUE UPDATED");
        }

        db.close();
    }
});

关于java - 在游标适配器中刷新ListView?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42153770/

10-09 09:44