我正在尝试在我的应用程序中实现搜索功能,但是我遇到了问题。
例如,当我搜索黄色花朵时,我发现200个结果,我将200个存储在 NumberWalls 变量中;以使适配器仅显示{{ 1}}。一切正常,我的问题是,当我搜索例如 Google图片时,我发现4个结果,而当我再次搜索黄色花朵时,我只看到4个结果RecyclerView
上的项目,而不是200。
RecyclerView
在适配器中:
private void setAdapterForRecyclerView(List<Wallpaper> wlls, int NumberWalls) {
if (myAdapter == null) {
errortv.setVisibility(View.GONE);
myAdapter = new MyAdapterSearch(wlls, getApplicationContext(), new RecyclerViewClickListener() {
@Override
public void onClick(View view, Wallpaper wallpaper) {
Intent intent = new Intent(Search.this, FullScreen.class);
intent.putExtra("img", wallpaper.getThumbnails());
intent.putExtra("tags", wallpaper.getTitle());
startActivity(intent);
}
@Override
public void onClick(View view, Category categories) {
}
}, (NumberWalls));
recyclerView.setAdapter(myAdapter);
} else {
int position = myAdapter.getItemCount();
myAdapter.getItems().addAll(wlls);
myAdapter.notifyItemRangeInserted(position, NumberWalls);
}
progressbar.setVisibility(View.GONE);
}
这是public MyAdapterSearch(List<Wallpaper> wallpapers, Context context, RecyclerViewClickListener clickListener, int numberItem) {
this.wallpapers = wallpapers;
this.context = context;
this.mClickListener = clickListener;
this.numberItem = numberItem;
}
.
.
.
@Override
public int getItemCount() {
return Math.min(wallpapers.size(), numberItem);
}
类,我在其中获得 NumberWalls :
AsyncTask
答案 0 :(得分:2)
我认为您可以放下numberItem
,让getItemCount()
简单地返回wallpapers.size()
。因为,您要为适配器提供搜索返回的壁纸列表,所以我想您要在列表中显示所有壁纸。
此外,在更新适配器时,无需将搜索结果添加到项目列表中,只需将结果设置为项目即可。
myAdapter.setWallpapers(wlls);
并将方法setWallpapers()
添加到适配器:
public void setWallpapers(List<Wallpaper> wallpapers) {
this.wallpapers = wallpapers;
notifyDatasetChanged();
}