I have a recycler view, inside the row of the recycler view I hava a text with 5 checkboxes. My problem is everytime I check one checkbox the Shared preference save all the checkboxes on that same spot. Here is my code of the adapter
public class HabitosAdapter extends RecyclerView.Adapter<HabitosAdapter.MyViewHolder> {
private List<Habitos> mHabitosList;
private Fragment mFragment;
private Context mContext;
private CheckBox[] checkBoxes = new CheckBox[5];
public class MyViewHolder extends RecyclerView.ViewHolder{
TextView nombre;
CardView habitosCardView;
CheckBox CB1, CB2, CB3, CB4, CB5 ;
public MyViewHolder(View itemView) {
super(itemView);
nombre = (TextView) itemView.findViewById(R.id.nombreHabito);
habitosCardView = itemView.findViewById(R.id.habitos_card_view);
CB1 = (CheckBox) itemView.findViewById(R.id.CBdia1);
CB2 = (CheckBox) itemView.findViewById(R.id.CBdia2);
CB3 = (CheckBox) itemView.findViewById(R.id.CBdia3);
CB4 = (CheckBox) itemView.findViewById(R.id.CBdia4);
CB5 = (CheckBox) itemView.findViewById(R.id.CBdia5);
}
}
public HabitosAdapter(Context mContext, List<Habitos> habitosList){
this.mContext = mContext;
this.mHabitosList = habitosList;
}
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.habitos_list_row, parent, false);
SharedPreferences preferences = mContext.getSharedPreferences("PROJECT_NAME", android.content.Context.MODE_PRIVATE);
boolean valueBoolean = preferences.getBoolean("KEY", false);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("KEY", valueBoolean);
editor.commit();
return new MyViewHolder(itemView);
}
public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position){
holder.nombre.setText(mHabitosList.get(position).getNombre());
holder.CB1.setChecked(getFromSP("CB1"));
holder.CB1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
saveInSp("CB1", b);
}
});
holder.CB2.setChecked(getFromSP("CB2"));
holder.CB2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(holder.CB2.isChecked()){
saveInSp("CB2", true);
} else{
saveInSp("CB2", false);
}
}
});
holder.CB3.setChecked(getFromSP("CB3"));
holder.CB3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
saveInSp("CB3", b);
}
});
holder.CB4.setChecked(getFromSP("CB4"));
holder.CB4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
saveInSp("CB4", b);
}
});
holder.CB5.setChecked(getFromSP("CB5"));
holder.CB5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
saveInSp("CB5",b);
}
});
}
public int getItemCount(){
return mHabitosList.size();
}
private boolean getFromSP(String key) {
SharedPreferences preferences = mContext.getSharedPreferences("PROJECT_NAME", android.content.Context.MODE_PRIVATE);
return preferences.getBoolean(key, false);
}
private void saveInSp(String key,boolean value){
SharedPreferences preferences = mContext.getSharedPreferences("PROJECT_NAME", android.content.Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(key, value);
editor.commit();
}
}
How can I save just the Checkbox that was checked? I know I have to get just the specific CheckBox that was checked and not set a listener on all the CheckBoxes2 but I dont know how to exactly do that, I am developing my first app, so thanks for your patience.
Aucun commentaire:
Enregistrer un commentaire