O
O
Orkhan Hasanli2018-06-03 02:39:29
Android
Orkhan Hasanli, 2018-06-03 02:39:29

How to resolve possible error in recyclerview after refreshing the list?

Hello!
I am using RecyclerView & Material SearchView to implement search.
Git - https://github.com/MiguelCatalan/MaterialSearchView
Search generally works, but there is one bug.
When you click on the first found result (on the first item), the first element of the list is opened, and not the first element of the search result ... And then, as if by clicking on the following elements, -1 elements are opened. Those. when you click on the second element, the first one opens, on the third --> the second, and so on. What could be wrong?
I suspect it's position, even though I'm using getAdapterPosition();
My adapter:
onBindViewHolder method

public void onBindViewHolder(MedicationsViewHolder holder, int position) {
        holder.bind(medicationList.get(holder.getAdapterPosition()));
}

onCreateViewHolder method
public MedicationsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View view = layoutInflater.inflate(R.layout.medication_item, parent, false);
        return new MedicationsViewHolder(view);
    }

ViewHolder
public class MedicationsViewHolder extends RecyclerView.ViewHolder {
        public TextView tradeNameText;
        public ImageButton imageButton;

        public MedicationsViewHolder(final View itemView) {
            super(itemView);
            tradeNameText = itemView.findViewById(R.id.medication_trade_name);
            imageButton = itemView.findViewById(R.id.favorite_button);
        }
        public void bind(Medication medication) {
            tradeNameText.setText(medication.get_tradeName());
            imageButton.setImageResource(R.drawable.ic_remove_from_fav);
        }

}

Search method:
public void setFilter(List<Medication> newList) {
        medicationList = new ArrayList<Medication>();
        medicationList.addAll(newList);
        notifyDataSetChanged();
    }

End-to-End Search Method:
public boolean onQueryTextChange(String newText) {
                if (newText != null && !newText.isEmpty()) {
                    List<Medication> newList = new ArrayList<Medication>();
                    newText = newText.toLowerCase();

                    for (Medication medication : medicationList) {
                        String tradeName = medication.get_tradeName().toLowerCase();
                        if (tradeName.contains(newText)) {
                            newList.add(medication);
                        }
                    }
                    medicationsAdapter.setFilter(newList);

               }  else {
                    medicationsAdapter = new MedicationsAdapter(medicationList, MainActivity.this);
                    recyclerView.setAdapter(medicationsAdapter);

                }
                return true;
            }

Click implementation:
// Слушатель для элементов recyclerView
        recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), recyclerView, new RecyclerTouchListener.ClickListener() {

            // Ловик клик при надобности
            @Override
            public void onClick(final View view, int position) {
                final RecyclerView.ViewHolder holder = recyclerView.findContainingViewHolder(view);


                // Ловим клик по препарату
                holder.itemView.findViewById(R.id.medication_trade_name).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent descriptionIntent = new Intent(MainActivity.this, DescriptionActivity.class);
                        descriptionIntent.putExtra("Medication", medicationList.get(holder.getAdapterPosition()));
                        startActivity(descriptionIntent);
                    }
                });

                final ImageButton imageButton = holder.itemView.findViewById(R.id.favorite_button);
                // Ловим клик по кнопке "Добавить в избранное"
                holder.itemView.findViewById(R.id.favorite_button).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(!db.isFavorite(valueOf(medicationList.get(holder.getAdapterPosition()).get_id()))) {
                            db.addToFavorites(valueOf(medicationList.get(holder.getAdapterPosition()).get_id()));
                            imageButton.setImageResource(R.drawable.ic_add_to_fav);
                        } else {
                            db.removeFromFavorites(valueOf(medicationList.get(holder.getAdapterPosition()).get_id()));
                            imageButton.setImageResource(R.drawable.ic_remove_from_fav);
                        }

                    }
                });

            }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan Hasanli, 2018-06-03
@azerphoenix

Denis Zagaevsky I'm sorry I can't choose your comment as an answer, but it really helped me. The problem was the listener.
Despite the fact that the click worked, but during the search, due to updating the list and changing the position, the click worked incorrectly.
Threw it into the onBindViewHolder() method

// Ловим клик по препарату
        holder.itemView.findViewById(R.id.medication_trade_name).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent descriptionIntent = new Intent(mContext, DescriptionActivity.class);
                descriptionIntent.putExtra("Medication", medicationList.get(holder.getAdapterPosition()));
                mContext.startActivity(descriptionIntent);
            }
        });

        final ImageButton imageButton = holder.itemView.findViewById(R.id.favorite_button);
        // Ловим клик по кнопке "Добавить в избранное"
        holder.itemView.findViewById(R.id.favorite_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!db.isFavorite(valueOf(medicationList.get(holder.getAdapterPosition()).get_id()))) {
                    db.addToFavorites(valueOf(medicationList.get(holder.getAdapterPosition()).get_id()));
                    imageButton.setImageResource(R.drawable.ic_add_to_fav);
                } else {
                    db.removeFromFavorites(valueOf(medicationList.get(holder.getAdapterPosition()).get_id()));
                    imageButton.setImageResource(R.drawable.ic_remove_from_fav);
                }

            }
        });

Responsibly, removed it from MainActivity. Now both clicking in the search and clicking in the list itself work correctly. I won’t say how clean it turned out from the point of view of programming, since I’m just learning so far)) But there are no double clicks)))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question