![](/img/trans.png)
[英]RecyclerView search filter getting wrong position after filtering result
[英]Recyclerview onclick show wrong data after do search and filtering
我试图使用编辑文本作为searchview使recyclerview具有很多数据。 单击该项目时,它还会显示数据。 但是,当我尝试搜索数据并单击它时,它将给出错误的数据或位置。 即使我已经删除了我搜索它的单词,也会给出错误的数据。
这是我的主适配器
public class AllBlockAdapter extends RecyclerView.Adapter<AllBlockAdapter.AllBlockViewHolder> {
private ArrayList<AllBlockItem> allBlockItemArrayList;
Context context;
public static class AllBlockViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public TextView textView1;
public TextView textView2;
private View itemView;
ArrayList<AllBlockItem> allBlockItems = new ArrayList<AllBlockItem>();
Context context;
public AllBlockViewHolder(View itemView, Context context, ArrayList<AllBlockItem> allBlockItems) {
super(itemView);
this.allBlockItems = allBlockItems;
this.context = context;
this.itemView = itemView;
imageView = itemView.findViewById(R.id.blockImage);
textView1 = itemView.findViewById(R.id.blockTitle);
textView2 = itemView.findViewById(R.id.blockRecipe);
}
}
public AllBlockAdapter(ArrayList<AllBlockItem> allBlockItems, Context context1) {
allBlockItemArrayList = allBlockItems;
context = context1;
}
@Override
public AllBlockAdapter.AllBlockViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.allblock_item, parent, false);
AllBlockViewHolder allBlockViewHolder = new AllBlockViewHolder(view, context, allBlockItemArrayList);
return allBlockViewHolder;
}
@Override
public void onBindViewHolder(final AllBlockAdapter.AllBlockViewHolder holder, int position) {
AllBlockItem currentItem = allBlockItemArrayList.get(position);
holder.imageView.setImageResource(currentItem.getImageResource());
holder.textView1.setText(currentItem.getText1());
holder.textView2.setText(currentItem.getText2());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = holder.getAdapterPosition();
AllBlockItem allBlockItem = holder.allBlockItems.get(position);
Intent intent = new Intent(this.context, AllBlockDetail.class);
intent.putExtra("mImageResource", allBlockItem.getImageResource());
intent.putExtra("mText1", allBlockItem.getText1());
intent.putExtra("mText2", allBlockItem.getText2());
this.context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return allBlockItemArrayList.size();
}
public void filterList(ArrayList<AllBlockItem> filteredList) {
allBlockItemArrayList = filteredList;
notifyDataSetChanged();
}
}
任何人都可以解决这个问题? 谢谢
我认为问题在于您没有使用正确的position值。换句话说,属于onBindViewHolder()方法的position参数应该仅在此方法内使用,因为由于过滤器操作后可能会更改的事实。 另一方面,使用getAdapterPosition()可确保获得正确的位置值。 因此,我认为,如果以正确的方式使用getAdapterPosition(),则问题将得到解决。 我已经修改了您的代码,使其可以正常工作。请注意带星号的注释。
公共类AllBlockAdapter扩展了RecyclerView.Adapter {private ArrayList allBlockItemArrayList; 上下文上下文;
public static class AllBlockViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public TextView textView1;
public TextView textView2;
//**** add this field****
private View itemView;
ArrayList<AllBlockItem> allBlockItems = new ArrayList<AllBlockItem>();
Context context;
public AllBlockViewHolder(View itemView, Context context, ArrayList<AllBlockItem> allBlockItems) {
super(itemView);
this.allBlockItems = allBlockItems;
this.context = context;
// ****initialize it here****
this.itemView = itemView;
imageView = itemView.findViewById(R.id.blockImage);
textView1 = itemView.findViewById(R.id.blockTitle);
textView2 = itemView.findViewById(R.id.blockRecipe);
}
}
public AllBlockAdapter(ArrayList<AllBlockItem> allBlockItems, Context context1) {
allBlockItemArrayList = allBlockItems;
context = context1;
}
@Override
public AllBlockAdapter.AllBlockViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.allblock_item, parent, false);
AllBlockViewHolder allBlockViewHolder = new AllBlockViewHolder(view, context, allBlockItemArrayList);
return allBlockViewHolder;
}
@Override
public void onBindViewHolder(final AllBlockAdapter.AllBlockViewHolder holder, int position) {
AllBlockItem currentItem = allBlockItemArrayList.get(position);
holder.imageView.setImageResource(currentItem.getImageResource());
holder.textView1.setText(currentItem.getText1());
holder.textView2.setText(currentItem.getText2());
//****set OnClickListener to it here ****
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// ****call getAdapterPosition() on the right ref****
int position = holder.getAdapterPosition();
AllBlockItem allBlockItem = holder.allBlockItems.get(position);
Intent intent = new Intent(holder.itemView.getContext(), AllBlockDetail.class);
intent.putExtra("mImageResource", allBlockItem.getImageResource());
intent.putExtra("mText1", allBlockItem.getText1());
intent.putExtra("mText2", allBlockItem.getText2());
holder.itemView.getContext().startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return allBlockItemArrayList.size();
}
public void filterList(ArrayList<AllBlockItem> filteredList) {
allBlockItemArrayList = filteredList;
notifyDataSetChanged();
}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.