gpt4 book ai didi

android - 更新 Recyclerview 的项目数

转载 作者:行者123 更新时间:2023-11-29 18:40:34 24 4
gpt4 key购买 nike

我正在尝试在我的应用程序中实现搜索功能,但我遇到了问题。

例如,当我搜索 yellow flowers 时,我找到了 200 个结果,我将 200 个存储在 NumberWalls 变量中,以使适配器在 中仅显示 200 个项目>RecyclerView。一切正常,我的问题是当我搜索例如 Google Picture 时,我找到了 4 个结果,当我再次搜索 yellow flowers 时,我在 RecyclerView 上只看到 4 个项目,而不是 200 个。

 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);
}

这是我得到 NumberWallsAsyncTask 类:

public class fetchDataSearch extends AsyncTask<Integer, List<Wallpaper>, List<Wallpaper>> {
Document doc = null;
ArrayList<Wallpaper> urlsSearch = new ArrayList<>();
String numberOnly;

@Override
protected void onPreExecute() {
super.onPreExecute();
}

@Override
protected List<Wallpaper> doInBackground(Integer... integers) {
try {
doc = Jsoup.connect(getResources().getString(R.string.SearchRequest) + mSearchText.replace(" ", "+") + "&page=" + integers[0]).get();
} catch (IOException e) {
e.printStackTrace();
}
if (doc != null) {
Element noResultfound = doc.getElementsByClass("notice notice-error").first();
if (noResultfound != null) {
noResultsFoundCatched = true;
} else {
Element element = doc.select("div.center > h1").first();
numberOnly = element.text().replaceFirst(".*?(\\d+).*", "$1");
Elements newsHeadlines = doc.getElementsByClass("img-responsive");
for (Element headline : newsHeadlines) {
String thumb = headline.select("img").attr("src");
String title = headline.select("img").attr("title");
Wallpaper wallpaperInfo = new Wallpaper();
wallpaperInfo.setThumbnails(thumb);
wallpaperInfo.setTitle(title);
if (BooleanChecker.checkTagsContains(title)) {
urlsSearch.add(wallpaperInfo);
} else {
unWantedTagsFound++;
}
}
noResultsFoundCatched = false;
}

} else {
noResultsFoundCatched = true;
}
return urlsSearch;
}

@Override
protected void onPostExecute(List<Wallpaper> wallpapersSearch) {
super.onPostExecute(wallpapersSearch);
if (numberOnly != null) {
numberWallpapersPage = Integer.parseInt(numberOnly);
setAdapterForRecyclerView(wallpapersSearch, numberWallpapers);
}
}
}

最佳答案

我认为您可以删除 numberItem 东西,让 getItemCount() 简单地返回 wallpapers.size()。因为,您要为适配器提供搜索返回的壁纸列表,所以我猜您想在列表中显示所有壁纸。

另外,更新适配器时,不是将搜索结果添加到项目列表,而是将结果设置为项目。

myAdapter.setWallpapers(wlls);

并将方法 setWallpapers() 添加到适配器:

public void setWallpapers(List<Wallpaper> wallpapers) {
this.wallpapers = wallpapers;
notifyDatasetChanged();
}

关于android - 更新 Recyclerview 的项目数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53140294/

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