samedi 6 juin 2020

How to store checkbox statuses after I exit app?

I have tab layout (3 fragments) with recyclerview and checkboxes for every fragment. I set up onClickListener, but after I exit my app, checkbox statuses are reseted. I tried some solutions I found online some solutions (about shared prefferences) but neither work for me. Maybe I implement it wrong. Can you help me ?
Also if need, I can share with you my Fragment file.

recyclerview java:

public class RecyclerViewAdapter extends RecyclerView.Adapter <RecyclerViewAdapter.MyViewHolder>{

    Context mContext;
    List<RecTab1> mData;

    public RecyclerViewAdapter(Context mContext, List<RecTab1> mData) {
        this.mContext = mContext;
        this.mData = mData;
    }
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v ;
        v = LayoutInflater.from(mContext).inflate(R.layout.item_tab1,parent,false);
        MyViewHolder vHolder = new MyViewHolder(v);
        return vHolder;
    }
    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, int position) {

        holder.tv_name.setText(mData.get(position).getName());
        holder.tv_subName.setText(mData.get(position).getSubName());
        holder.cb_checkbox.setChecked(mData.get(position).getSelected());
        holder.cb_checkbox.setTag(position);
        holder.cb_checkbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Integer pos = (Integer) holder.cb_checkbox.getTag();
                Toast.makeText(mContext, mData.get(pos).getName() + " clicked!", Toast.LENGTH_LONG).show();
                if (mData.get(pos).getSelected()) {
                    mData.get(pos).setSelected(false);
                } else {
                    mData.get(pos).setSelected(true);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }
    public static class MyViewHolder extends RecyclerView.ViewHolder{

        private TextView tv_name;
        private TextView tv_subName;
        private CheckBox cb_checkbox;

        public MyViewHolder (View itemView){
            super (itemView);
            tv_name = (TextView) itemView.findViewById(R.id.Item1Name);
            tv_subName = (TextView) itemView.findViewById(R.id.Item1SubName);
            cb_checkbox = (CheckBox) itemView.findViewById(R.id.CheckboxID);
        }
    }
}

item java:

public class RecTab1 {
    private String Name;
    private String SubName;
    private boolean isSelected;

    public RecTab1(){
    }
    public RecTab1 (String name, String subName){
        Name = name;
        SubName = subName;
    }
    public String getName() {
        return Name;
    }
    public String getSubName() {
        return SubName;
    }
    public void setName(String name) {
        Name = name;
    }
    public void setSubName(String subName) {
        SubName = subName;
    }
    public boolean getSelected() {
        return isSelected;
    }
    public void setSelected(boolean selected) {
        isSelected = selected;
    }
}



Aucun commentaire:

Enregistrer un commentaire