I have a RecyclerView and I have created an adapter RecyclerView.Adapter for it. My RecyclerView Item contains one TextView, one Checkbox. If the user clicks the checkbox I show a cross through the text. But when I click one checkbox, some other checkboxes are also clicked. My BindView Code
public void onBindViewHolder(Tasks_View_Holder holder, int position) {
final Tasks_View_Holder holder_final = holder;
final int position_final = position;
Tasks task = list.get(position);//list is my recyclerview
holder.checkBox.setChecked(task.getChecked());
if (holder.checkBox.isChecked()) {
holder.taskname.setPaintFlags(holder.taskname.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
} else {
holder.taskname.setPaintFlags(0);
}
holder.taskname.setText(task.getTaskname());
holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.d("bhaskar", "on check changed checkbox" + position_final);
Tasks task = list.get(position_final);
task.setChecked(isChecked);
list.set(position_final, task);
if (isChecked) {
holder_final.taskname.setPaintFlags(holder_final.taskname.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
} else {
holder_final.taskname.setPaintFlags(0);
}
updateTask(task.getTasknumber(), task.getTaskname(), task.getChecked());// a method that updates the task in internal database
Handler handler = new Handler();
final Runnable r = new Runnable() {
public void run() {
Log.d("bhaskar", "notify data item changed checkbox" + position_final);
notifyItemChanged(position_final);
}
};
handler.post(r);
}
});
}
Aucun commentaire:
Enregistrer un commentaire