Check box in recyclerview when loading

I'm creating a recyclerview containing projects. Each project has one Check@R_ 676_ 2419 @, imagebutton and textview, as follows:

public class RecyclerViewViewHolder extends RecyclerView.ViewHolder {
    RelativeLayout item;
    TextView id;
    TextView name;
    ImageButton delete;
    Check@R_676_2419@ check@R_676_2419@;

    public RecyclerViewViewHolder(View view) {
        super(view);

        item = (RelativeLayout) view.findViewById(R.id.layout_item_item);
        id = (TextView) view.findViewById(R.id.layout_item_id);
        name = (TextView) view.findViewById(R.id.layout_item_name);
        delete = (ImageButton) view.findViewById(R.id.layout_item_delete);
        check@R_676_2419@ = (Check@R_676_2419@) view.findViewById(R.id.layout_item_check@R_676_2419@);
    }
}

I want to select the check box view only when a value in the database column is equal to 1. If the value is 0, it should not be selected. I start with the following code:

sqliteDatabaseAdapter database = new sqliteDatabaseAdapter(context);
    database.open();

    Cursor cursor = database.getAllItems(table);

    if (cursor.moveToFirst()) {
        do {
            if (cursor.getInt(2) == 0) {
                viewHolder.check@R_676_2419@.setChecked(false);
            }

            else {
                viewHolder.check@R_676_2419@.setChecked(true);
            }

        } while (cursor.moveToNext());

    }

    database.close();

The code is valid, but it checks the first row in the database, and if the checked column is 1, it changes each column to be checked Check@R_ 676_ 2419@. I just want to check the information associated with a particular row Check@R_ 676_ 2419 @, not every line. What should I do?

resolvent:

I suggest you create a HashMap < > and link the ID and checked status. Then, you can reference and get the checked status from HashMap < >

HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>();

Then, in your onbindviewholder function, you can use the following command to check your Check@R_ 676_ 2419@:

if (hashMap.get(position) == 0) {
    // not checked
}

else {
    // checked
}

Make sure to add ID and checked status from SQLite database

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>