Answer the question
In order to leave comments, you need to log in
How to fix incorrect display of featured items in recyclerview?
Good day!
Below is the code I am using to add or remove from favorites
imageButton = (ImageButton) findViewById(R.id.favorite_button);
// if(db.isFavorite(valueOf(medicationList.get(position).get_id())))
// imageButton.setImageResource(R.drawable.ic_add_to_fav);
// Ловим клик по кнопке "Добавить в избранное"
holder.itemView.findViewById(R.id.favorite_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!db.isFavorite(valueOf(medicationList.get(position).get_id()))) {
db.addToFavorites(valueOf(medicationList.get(position).get_id()));
imageButton.setImageResource(R.drawable.ic_add_to_fav);
} else {
db.removeFromFavorites(valueOf(medicationList.get(position).get_id()));
imageButton.setImageResource(R.drawable.ic_remove_from_fav);
}
}
});
medicationList.get(position).get_id()
bothmedicationList.get(holder.getAdapterPosition()).get_id()
// if(db.isFavorite(valueOf(medicationList.get(position).get_id())))
// imageButton.setImageResource(R.drawable.ic_add_to_fav);
Answer the question
In order to leave comments, you need to log in
hmm guys don't work with setting onClickListener to onBindViewHolder this time. Secondly, if you really want to work, put it in a holder, and simply change the data on which the click should work in onBindViewHolder. Thirdly, the holder is designed, among other things, to find all significant fields once in the constructor and associate them with the layout, and not to call findViewById every time in onBindViewHodler. No need to work in RecyclerView with position. You need to work with data in the holder.
hmm, I'd suggest you look into how anonymous classes capture variables - see here for example .
the reason for the error is that you are using position, which is passed as a parameter to the onBindViewHolder method. here it is worth using holder.getAdapterPosition() inside an anonymous click handler class.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question