Answer the question
In order to leave comments, you need to log in
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()));
}
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);
}
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);
}
}
public void setFilter(List<Medication> newList) {
medicationList = new ArrayList<Medication>();
medicationList.addAll(newList);
notifyDataSetChanged();
}
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;
}
// Слушатель для элементов 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
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);
}
}
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question