I have a tasks app where the person can click on the checkbox of the task and the colour of the task will change. But i'm having a problem with binding this condition with the view. When the checkbox is checked, the colour changes but when the app is closed and opened again, the checkbox is not checked and the color goes back to normal. How can i retain the checked state of the checkbox and the text colour in this case.
My Adapter class -
public void onBindViewHolder(@NonNull TaskHolder holder, int position) {
Task currentTask = tasks.get(position);
holder.a_tname.setText(currentTask.getTname());
holder.a_tdate.setText(currentTask.getTDate());
holder.a_ttime.setText(currentTask.getTTime());
holder.a_tprior.setText(currentTask.getTprior());
holder.bind(tasks.get(position));
holder.checkbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.bind2(tasks.get(position));
}
});
}
class TaskHolder extends RecyclerView.ViewHolder {
private final TextView a_tname;
private final TextView a_tdate;
private final TextView a_ttime;
private final TextView a_tprior;
ImageView priorityIndicator;
CheckBox checkbox;
public TaskHolder(View itemView) {
super(itemView);
a_tname = itemView.findViewById(R.id.a_tname);
a_tdate=itemView.findViewById(R.id.a_tdate);
a_ttime = itemView.findViewById(R.id.a_ttime);
a_tprior = itemView.findViewById(R.id.a_tprior);
priorityIndicator = itemView.findViewById(R.id.priorityIndicator);
checkbox = itemView.findViewById(R.id.checkbox);
private void bind2(Task task){
if(checkbox.isChecked()){
int checkedtext = ContextCompat.getColor(a_tname.getContext(), R.color.grey);
a_tname.setTextColor(checkedtext);
int checkeddate = ContextCompat.getColor(a_tdate.getContext(), R.color.grey);
a_tdate.setTextColor(checkeddate);
int checkedtime = ContextCompat.getColor(a_ttime.getContext(), R.color.grey);
a_ttime.setTextColor(checkedtime);
Toast.makeText(checkbox.getContext(), "Way to go! Now swipe to delete", Toast.LENGTH_LONG).show();
}
if(!checkbox.isChecked()){
int untext = ContextCompat.getColor(a_tname.getContext(), R.color.black);
a_tname.setTextColor(untext);
int undate = ContextCompat.getColor(a_tdate.getContext(), R.color.black);
a_tdate.setTextColor(undate);
int untime = ContextCompat.getColor(a_ttime.getContext(), R.color.black);
a_ttime.setTextColor(untime);
}
}
May i please know how this can be done