Reputation: 923
I've built a recyclerview that displays items from an sqlite db within the application. There is an imageview with an icon that changes when the user clicks on the item as the item is now considered read. My sqlite table which stores this data has a column with a read status, by default everything is unread and when the user clicks and item that status is changed to read.
This status is how the adapter class knows initially which icon to use. This part all works. The problem is when the user clicks an item and the icon changes to the new read icon, it then changes back to the original unread.
@Override
public void onBindViewHolder(@NonNull final MyViewHolder holder, int position) {
final Reply reply = ReplyList.get(position);
if(reply.getStatus().equals("unread")){
holder.status.setImageResource(R.drawable.ic_unread);
holder.status.setColorFilter(context.getResources().getColor(R.color.red));
} else {
holder.status.setImageResource(R.drawable.ic_read);
holder.status.setColorFilter(context.getResources().getColor(R.color.green));
}
holder.name.setText(smsReply.getName());
holder.patientNumber.setText(smsReply.getNumber());
holder.dateReceived.setText(smsReply.getReceivedDate());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
DBHandler db = new DBHandler(view.getContext());
db.changeReadStatus(smsReply.getId());
holder.status.setImageResource(R.drawable.ic_read);
holder.status.setColorFilter(context.getResources().getColor(R.color.green));
notifyItemChanged(holder.getAdapterPosition());
//notifyDataSetChanged();
}
I first just changed the image by calling
holder.status.setImageResource(R.drawable.ic_read);
holder.status.setColorFilter(context.getResources().getColor(R.color.green));
this works until I scroll past the item, when I come back to the item the icon has changed back to the unread icon, I figured this was due to the caching nature of a recyclerview view.
I then added notifyItemChanged(holder.getAdapterPosition());
which changes the image then it changes back to the unread one for some reason.
I tried adding notifyDataSetChanged();
after that but that simply does nothing, there is no change at all.
I'm not sure what else to try ?
Upvotes: 1
Views: 263
Reputation: 2141
add this code while changing the image
reply.setStatus("read");
Upvotes: 1