samedi 20 avril 2019

Retaining CheckBox state within RecyclerView which is feed by SQL Database

Good evening everyone

Brief overview before a greater explanation. I currently have a SQL database which feeds into an arraylist thanks to a modal, we then feed this arraylist to our recycle view adapter which then populates the row.

Now i'm trying to build in the option for the user to select a row and then a check becomes true and tick is displayed. As you all currently know if the state is not stored then it start moving to random rows or being removed completely.

Lets start at the beginning with my SQL Lite database. Three simple rows Title , Description and checkbox state. Note that when a new row is added to the sql he checkbox column is automatically set to false.

Snippet below of the SQL query used to populate my recyclerView adapter

ArrayList<NoteInfoModal> noteInfoModalArrayList = new ArrayList<>();
    SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
    Cursor cursor =  sqLiteDatabase.rawQuery("SELECT * FROM " + Primary_Table + " ORDER BY Col_DateOrder ASC",   null);
    cursor.moveToFirst();


    while (!cursor.isAfterLast()){

        NoteInfoModal noteInfoModal = new NoteInfoModal();
        noteInfoModal.setNoteName(cursor.getString(cursor.getColumnIndex(Col_NoteTitle)));

Applying it to the adapter

homeScreenAdapter = new HomeScreenAdapter(getContext(), primaryDatabase.populateHomeScreenOldestFirst(), filterOption, HomeScreenFragment.this);

Finally binding the information inside the adapter to the correct view (This one is for the check box)

((CustomViewHolder) holder).chkNote.setChecked(Boolean.valueOf(noteInfoModal.noteCheckBox));

Now I understand that I could simply update the row of the SQL DB to be true as this will then save the state, but if the user closes and then opens the app again I want them all to unchecked / false each time

Just looking for another way of approaching this issue.

Thank you




Aucun commentaire:

Enregistrer un commentaire