O
O
Orkhan Hasanli2018-05-28 02:25:07
Android
Orkhan Hasanli, 2018-05-28 02:25:07

Is there an "analog" for adapter.getRef(position).getKey() in RecyclerView.Adapter?

Hello!
Is there any "analogue" adapter.getRef(position).getKey()that can be used in RecyclerView.Adapter?
Digging around the net, I found only one solution:
https://stackoverflow.com/questions/50283774/getre...
As for my code, it is presented below (in the onbindviewholder method):

holder.imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!db.isFavorite(adapter.getRef(position).getKey())) {
                    db.addToFavorites(adapter.getRef(position).getKey());
                    holder.imageButton.setImageResource(R.drawable.ic_add_to_fav);
                    Toast.makeText(mContext, medication.get_tradeName() + " добавлен в избранное", Toast.LENGTH_SHORT).show();
                } else {
                    db.removeFromFavorites(adapter.getRef(position).getKey());
                    holder.imageButton.setImageResource(R.drawable.ic_remove_from_fav);
                    Toast.makeText(mContext, medication.get_tradeName() + " удален из избранного.", Toast.LENGTH_SHORT).show();
                }
            }
        });

Maybe someone knows a simpler solution? Thank you in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Perelygin, 2018-05-28
@azerphoenix

Ideally, onBind does not need to add an OnClickListener to the button. That's right - it's to add your own implementation of SimpleOnItemTouchListener and using GestureDetector and onGestureListener and catch a touch on the element, and then look, if you need where the touch fell and change accordingly in the adapter for this element and initialize new data using notifyItemDataChanged. and in onBind only bind data to the holder.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question