Search code examples
javaandroidlistviewsimplecursoradapter

Refreshing my ListView within my Cursor Adapter?


Long time lurker - first post:

Problem: When I click an imagebutton (basically just checkboxes) they will update my database row correctly and change the image to the proper boolean. However when I click them again - it does not re-update and gives off the same message.

What I think the problem is: I am rather new to Android but I'm pretty sure while my database is updating correctly my bindview variables are not?

My Cursor Adapter 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();
            }
        }
    });

What I have tried: So I click an image and it'll go from "checked" to "unchecked" or visa versa however only allows it once unless I reload the list.

What I've tried: Requery kept coming up but that is now considered outdated and to be avoided. I saw a few posts talking about swapping out the query for a new one but I kept getting a null error so I am unsure if that is the answer and I am just misunderstanding something or what.

Thanks, appreciate you guys.


Solution

  • if statement inside of OnClickListener is not checking with the latest value of status column. You need to read it again inside of OnClickListener. Change code like this:

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