How to set checkboxes in Recyclerview when user exits application and returns? (Android Java)

164 views Asked by At

I am currently using SharedPreferences key, value pairs to save the state, but can't seem to read from the prefs file and check the boxes.

I have written this self-explanatory code. However, a NullPointerException is thrown in the line list.findViewHolderForAdapterPosition(i).itemView.performClick();. I just want to know how to set the checkboxes in Recyclerview list to checked and save that state. Please help.

btnGetSelected.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        StringBuilder stringBuilder = new StringBuilder();
        for (Number number : numbers) {
            if (number.isSelected()) {
                if (stringBuilder.length() > 0)
                    stringBuilder.append(", ");
                stringBuilder.append(number.getTextONEs());
                Log.e("Checked", "here");
                setPreference(getApplicationContext(), "1", number.getONEs());
            } else {
                Log.e("Not checked", "here");
                setPreference(getApplicationContext(), "0", number.getONEs());
            }
        }
    }
});
try {
    for (int i = 0; i < tocArray.length; i++) {
        String value = getPreference(getApplicationContext(),Integer.toString(i));
        if (value.equalsIgnoreCase("1")) {
            list.findViewHolderForAdapterPosition(i).itemView.performClick();
        } else {
            //Do Nothing
        }
    }
} catch (Exception e) {
    e.printStackTrace();
}

This is from the holder of the Recyclerview;

@Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.bindData(numbers.get(position));

        holder.checkbox.setOnCheckedChangeListener(null);

        holder.checkbox.setChecked(numbers.get(position).isSelected());

        holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                numbers.get(holder.getAdapterPosition()).setSelected(isChecked);
            }
        });

Can you also please explain this whole concept of recyclerview and cards and why the "holder" is needed? Thanks. Please help! Regards

1

There are 1 answers

0
i_A_mok On

Try add these methods to your Activity:

private void restoreSelectedPositions(){
    SharedPreferences preferences = getSharedPreferences("selected_pos", MODE_PRIVATE);
    int tmpTotal = preferences.getInt("total_pos", 0);
    if(tmpTotal == numbers.size()){
        for(int i=0; i<tmpTotal; i++{
            boolean tmpFlag = preferences.getBoolean("pos" + i, false);
            numbers.get(i).setSelected(tmpFlag);
        }
    }else{
        // Saved data size not match with existing data.
    }
}

private void saveSelectedPositions(){
    SharedPreferences preferences = getSharedPreferences("selected_pos", MODE_PRIVATE);
    SharedPreferences.Editor editor = preferences.edit();
    editor.clear();
    editor.putInt("total_pos", numbers.size());
    for(int i=0; i<numbers.size(); i++){
        editor.putBoolean("pos" + i, numbers.get(i).isSelected());
    }
    editor.commit();
}

Change onResume() of Activity:

@Override
protected void onResume() {
    super.onResume();
    restoreSelectedPositions();
    // Change the line below.
    Your RecyclerViewAdapter.notifyDataSetChanged();
}

When you need to save, just call saveSelectedPositions(). Hope this help!