gpt4 book ai didi

android - RecyclerView 搜索过滤器位置错误

转载 作者:太空宇宙 更新时间:2023-11-03 13:13:18 25 4
gpt4 key购买 nike

我使用 RecyclerView 并对其进行了过滤,但我遇到了问题

在我搜索后,当我按下项目时,我得到了错误的位置,不是我搜索过的,请帮助我解决这个问题

RecyclerView 适配器

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

Context context;
List<listitem_gib> getDataAdapter;



public RecyclerViewAdapter(List<listitem_gib> getDataAdapter, Context context){

super();

this.getDataAdapter = getDataAdapter;
this.context = context;

}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_layout, parent, false);

ViewHolder viewHolder = new ViewHolder(v,context,getDataAdapter);

return viewHolder;
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {

listitem_gib getDataAdapter1 = getDataAdapter.get(position);

holder.name.setText(getDataAdapter1.getName());
holder.num.setText(getDataAdapter1.getnum());
Picasso.with(context).load("http://i-geeky.info/android/image/" + getDataAdapter1.getimg()).into(holder.img1);


}

@Override
public int getItemCount() {

return getDataAdapter.size();
}

class ViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener{

public TextView name;
public TextView num;
public ImageView img1;

ImageButton fav;

Context context;
List<listitem_gib> getDataAdapter;


public ViewHolder(View itemView, Context context ,List<listitem_gib> getDataAdapter ) {

super(itemView);
itemView.setOnClickListener(this);
this.getDataAdapter = getDataAdapter;
this.context = context;
this.fav= (ImageButton) itemView.findViewById(R.id.btn_fav);

name = (TextView) itemView.findViewById(R.id.Main_Text) ;
num = (TextView) itemView.findViewById(R.id.Second_Text) ;
img1 = (ImageView) itemView.findViewById(R.id.img1) ;

}


@Override
public void onClick(View v) {

int position = getAdapterPosition();
listitem_gib getDataAdapter =this.getDataAdapter.get(position);
Intent intent = new Intent(this.context,Rewaya_info.class);
intent.putExtra("name",getDataAdapter.getName());
intent.putExtra("url",getDataAdapter.geturl());
intent.putExtra("img",getDataAdapter.getimg());
intent.putExtra("num",getDataAdapter.getnum());
intent.putExtra("size",getDataAdapter.getsize());
this.context.startActivity(intent);

}
}
public void setFilter(List<listitem_gib> newList)
{
getDataAdapter = new ArrayList<>();
getDataAdapter.addAll(newList );
notifyDataSetChanged();
}}

Activity

@Override
public boolean onQueryTextChange(String newText) {

newText = newText.toLowerCase();
List<listitem_gib> newList = new ArrayList<>();
for (listitem_gib list : GetDataAdapter1){
String name = list.getName().toLowerCase();
String name1 = list.getnum().toLowerCase();

if (name.contains(newText) || name1.contains(newText)){
newList.add(list);
}
}
recyclerViewadapter.setFilter(newList);
return true;
}

最佳答案

recyclerview 重用现有元素并简单地将新数据绑定(bind)到这些元素——这可以防止需要创建和销毁的项目,而是回收它们;)。

查看ViewHolder 的构造函数。在其中您传入您的 ViewHolder 绑定(bind)到的初始数据列表。实际上,这会创建一个静态绑定(bind)到您的初始列表,不是过滤后的列表:

public ViewHolder(View itemView, Context context ,List<listitem_gib> getDataAdapter ) {
super(itemView);
itemView.setOnClickListener(this);
this.getDataAdapter = getDataAdapter; <-- this is stored in the viewholder
...
}

更好的方法是更好地利用 RecyclerViewAdapteronBindViewHolder。这将允许 ViewHolder 在单击时使用适配器中的当前项,如下所示:

@Override 
public void onBindViewHolder(final ViewHolder holder, final int position) {

listitem_gib item = getDataAdapter.get(position);
holder.bind(item);
}

按如下方式更新您的 ViewHolder:

class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

private TextView name;
private TextView num;
private ImageView img1;

ImageButton fav;

Context context;
private listitem_gib currentItem; //<-- allows this viewholder to keep a reference to the bound item

public ViewHolder(View itemView, Context context) { //<-- no more adapter in constructor
super(itemView);
itemView.setOnClickListener(this);
this.context = context;
this.fav = (ImageButton) itemView.findViewById(R.id.btn_fav);
name = (TextView) itemView.findViewById(R.id.Main_Text) ;
num = (TextView) itemView.findViewById(R.id.Second_Text) ;
img1 = (ImageView) itemView.findViewById(R.id.img1) ;
}

void bind (listitem_gib item) { //<--bind method allows the ViewHolder to bind to the data it is displaying
name.setText(item.getName());
num.setText(item.getnum());
Picasso.with(this.context).load("http://i-geeky.info/android/image/" + item.getimg()).into(holder.img1);
currentItem = item; //<-- keep a reference to the current item
}

@Override
public void onClick(View v) {
Intent intent = new Intent(this.context,Rewaya_info.class);
intent.putExtra("name",currentItem.getName()); // <-- makes use of currentItem instead of adapter
intent.putExtra("url",currentItem.geturl());
intent.putExtra("img",currentItem.getimg());
intent.putExtra("num",currentItem.getnum());
intent.putExtra("size",currentItem.getsize());
this.context.startActivity(intent);
}
}

祝你好运!

关于android - RecyclerView 搜索过滤器位置错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40747468/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com