0

I used checkBox in Recycler View , While checkBox is selected ArrayList favorite is filled up with new Item from ArrayList list_items according to position , when click on favorite button the Recycler View is filled with ArrayList favorite but selection of checkBoxes is gone , and when onBackPressed is selected Recycler View appear filled with ArrayList list_items without selected checkBoxes , I tried shared preferences to solve this and also keep checkBoxes selection if I close the app but didn't succeed

    class RecyclerView_dAdapter extends RecyclerView.Adapter {

private List<List_Item> list_items;
private ArrayList favorite = new ArrayList();
private List<List_Item> favorite_itemes = new ArrayList<>();
private Context context;

RecyclerView_dAdapter(List<List_Item> list_Item, Context context) {
list_items = list_Item;
this.context = context;
}

@Override
public RecyclerView_dAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item, parent, false);
return new ViewHolder(v);
}

@Override
public void onBindViewHolder(final RecyclerView_dAdapter.ViewHolder holder, final int position) {
final List_Item current = list_items.get(position);
final String name = current.getName();
holder.TextName.setText(name);

 holder.TextName.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(context, Show_Team_Activity.class);
        intent.putExtra("Team_num", name);
        context.startActivity(intent);
    }
});
holder.favorite_checkBox.setChecked(favorite_itemes.contains(current)); 
holder.setItemClickListener(new ItemClickListener() {
    @Override
    public void onItemClick(View v, int pos) {
        CheckBox chk = (CheckBox) v;
        if (chk.isChecked()) {
           favorite_itemes.add(list_items.get(pos));
            chk.setChecked(true);
        } else if (!chk.isChecked()) {               
            favorite_itemes.remove(list_items.get(pos));
            chk.setChecked(false);
        }
    }
});

}
ArrayList favorite_array(){
for (int i = 0; i < favorite_itemes.size(); i++){
    favorite.add(favorite_itemes.get(i));
}return favorite;
}

@Override
public int getItemCount() {
return list_items.size();
}

class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {      
private TextView TextName;
private CheckBox favorite_checkBox;
ItemClickListener itemClickListener;
ViewHolder(View view) {
    super(view);
    TextName = (TextView) view.findViewById(R.id.textView_rowitem);
    favorite_checkBox = (CheckBox) view.findViewById(R.id.favorite_checkBox);
   favorite_checkBox.setOnClickListener(this);
}
public void setItemClickListener(ItemClickListener ic) {
    this.itemClickListener = ic;
}

@Override
public void onClick(View v) {
    this.itemClickListener.onItemClick(v, getLayoutPosition());
}
}
 }
Mohamed Ahmed
  • 57
  • 1
  • 10

2 Answers2

0

Use these steps: 1. Convert your favourite_items to as JSONArray and convert it to a string

  1. Store the String in SharedPreferences

  2. In the on create retrieve the Shared preferences, convert it back to JSON Array and then to the ArrayList

  3. Traverse if the ArrayList contains the current check box, mark it as selected.

Have alook at this https://stackoverflow.com/a/10238647/2555419

Joey Pinto
  • 1,735
  • 1
  • 18
  • 34
0

Managing two lists inside a RecyclerAdpater is somehow a bad approach

anyway, could you post your List_Item class? without knowing your data model structure we won't be able to give any suggestions, however we can still expect how it looks like :

  • If your List_Item has an id, you can store it to a favourite SharedPreferences and check later if it contains this id.
Tamim Attafi
  • 2,253
  • 2
  • 17
  • 34