mercredi 5 mai 2021

How to save checkbox state in recyclerview / android studio

what i want is that when someone click in checkbox the state is saved and just the checkbox that was clicked stay checked , the problem that i found out: when i click in checkbox and starts another activity and go back all the checkbox are checked, how can i make just the specific checkbox to be checked.

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

Context context;
ArrayList<Mydoes> mydoes;


public  AdapterDoes(Context c, ArrayList<Mydoes> p){
   context = c;
   mydoes = p;

}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup ViewGroup, int i) {
    return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_does, ViewGroup, false));
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
    myViewHolder.titleDoes.setText(mydoes.get(i).getTitleDoes());
    myViewHolder.DescribeDoes.setText(mydoes.get(i).getDescribeDoes());
    myViewHolder.dateDoes.setText(mydoes.get(i).getDateDoes());
    myViewHolder.alarm.setText(mydoes.get(i).getAlarm());

    if (myViewHolder.checkBox2.isChecked()){
        myViewHolder.titleDoes.setAlpha(0.4f);
        myViewHolder.titleDoes.setPaintFlags(myViewHolder.titleDoes.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean("checkBox", myViewHolder.checkBox2.isChecked()).commit();
    }else{
        myViewHolder.titleDoes.setAlpha(1f);
        PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean("checkBox", myViewHolder.checkBox2.isChecked()).commit();

    }


    myViewHolder.checkBox2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (myViewHolder.checkBox2.isChecked()){
                myViewHolder.titleDoes.setAlpha(0.4f);
                myViewHolder.titleDoes.setPaintFlags(myViewHolder.titleDoes.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

                PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean("checkBox", myViewHolder.checkBox2.isChecked()).commit();
            } else if (!myViewHolder.checkBox2.isChecked()){
                myViewHolder.titleDoes.setAlpha(1f);
                PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean("checkBox", myViewHolder.checkBox2.isChecked()).commit();
            }
        }
    });



    myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {


        final String getTitleDoes = mydoes.get(i).getTitleDoes();
        final String getDescribeDoes = mydoes.get(i).getDescribeDoes();
        final String getDateDoes = mydoes.get(i).getDateDoes();
        final String getAlarm = mydoes.get(i).getAlarm();
        final String getKeyDoes = mydoes.get(i).getKeyDoes();


        @Override
        public void onClick(View v) {
            Intent i = new Intent(context,Edit_Check.class);
            i.putExtra("titleDoes",getTitleDoes);
            i.putExtra("DescribeDoes",getDescribeDoes);
            i.putExtra("dateDoes",getDateDoes);
            i.putExtra("alarm",getAlarm);
            i.putExtra("keyDoes",getKeyDoes);
            context.startActivity(i);
        }
    });



}

@Override
public int getItemCount() {

    return mydoes.size();
}

class MyViewHolder extends RecyclerView.ViewHolder {

    TextView titleDoes,DescribeDoes,dateDoes, alarm, keyDoes;
    CheckBox checkBox2;


    public MyViewHolder(@NonNull View itemView) {
        super(itemView);
        titleDoes = (TextView) itemView.findViewById(R.id.titleDoes);
        DescribeDoes = (TextView) itemView.findViewById(R.id.DescribeDoes);
        dateDoes = (TextView) itemView.findViewById(R.id.dateDoes);
        alarm = (TextView) itemView.findViewById(R.id.alarm);
        checkBox2 = (CheckBox) itemView.findViewById(R.id.checkBox2);

        boolean checked = PreferenceManager.getDefaultSharedPreferences(context).getBoolean("checkBox", false);
        checkBox2.setChecked(checked);

    }
}

}




Aucun commentaire:

Enregistrer un commentaire