O
O
Orkhan Hasanli2018-05-30 03:51:23
Android
Orkhan Hasanli, 2018-05-30 03:51:23

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);
                            
                        }

                    }
                });

The very function of writing and deleting the id of the selected element in the database works correctly, but there is a problem with the ImageButton. Regardless of which element was selected, the button is always selected as "favorite" only for the first element)))))
I tried, medicationList.get(position).get_id()both
medicationList.get(holder.getAdapterPosition()).get_id()

What's my mistake? Why is only one and the same element selected instead of the selected one? (I repeat that the selected element itself is added to the database correctly)
And another incidental question:
there is a small line of code, which, in theory, should mark all elements that are selected in the onCreate method.
//                if(db.isFavorite(valueOf(medicationList.get(position).get_id())))
//                    imageButton.setImageResource(R.drawable.ic_add_to_fav);

But it also somehow does not work out (
I would be grateful for help or for a hint

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Perelygin, 2018-05-30
@azerphoenix

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.

Y
Yuri, 2018-05-30
@YuryBorodkin

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.

D
davidnum95, 2018-05-30
@davidnum95

notifyDataSetChanged or notifyItemChanged ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question