gpt4 book ai didi

java - 如何清除searchview中的recyclerview/adapter以进行下一次搜索android

转载 作者:行者123 更新时间:2023-12-02 03:17:54 28 4
gpt4 key购买 nike

我正在使用改造来搜索图像并显示。我正在使用 SearchView 进行搜索,并使用 recyclerview 和适配器来显示该项目。

这是第一次工作。当我第二次搜索时,它显示相同的项目。它没有清除要显示的 View 。

这是我的代码:

    public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getName();
ImagesViewModel newsViewModel;
List<Hits> newsArticles;
List<Hits> articleArrayList;
ImagesAdapter newsAdapter;
RecyclerView rvHeadline;
SearchView searchView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rvHeadline = (RecyclerView) findViewById(R.id.rvNews);
searchView=(SearchView) findViewById(R.id.searchView);
searchView.setQueryHint("Search Images");
searchView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
searchView.setIconified(false);
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

@Override
public boolean onQueryTextSubmit(String query) {
callPixabayImages(query);
Toast.makeText(getBaseContext(), query, Toast.LENGTH_LONG).show();
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
callPixabayImages(newText);
Toast.makeText(getBaseContext(), newText, Toast.LENGTH_LONG).show();
return false;
}
});
articleArrayList = new ArrayList<>();
}

private void callPixabayImages(String query){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ImageApiInterface.PIXABAY_URL)
.addConverterFactory(GsonConverterFactory.create()) //Here we are using the GsonConverterFactory to directly convert json data to object
.build();

ImageApiInterface api = retrofit.create(ImageApiInterface.class);

Call<MyImages> call = api.getMovieDetails(query);
call.enqueue(new Callback<MyImages>() {
@Override
public void onResponse(Call<MyImages> call, Response<MyImages> response) {
Log.e("response","response"+response.code());
if(response.isSuccessful()){
Log.e("response","response"+response.code());
if((response.body() != null ? response.body().getHits() : null) !=null){
List<Hits> imagesList = response.body().getHits();
articleArrayList.addAll(imagesList);
newsAdapter.notifyDataSetChanged();
}
}



}

@Override
public void onFailure(Call<MyImages> call, Throwable t) {
System.out.println("Error: "+ t.getMessage());
Log.e("response","response"+t);
}
});

}
private void setupRecyclerView() {
if (newsAdapter == null) {
newsAdapter = new ImagesAdapter(MainActivity.this, articleArrayList);

rvHeadline.setLayoutManager(new LinearLayoutManager(this));
rvHeadline.setAdapter(newsAdapter);
rvHeadline.setItemAnimator(new DefaultItemAnimator());
rvHeadline.setNestedScrollingEnabled(true);
} else {
newsAdapter.notifyDataSetChanged();
}
}

在这里,我需要重置以搜索新项目,以避免显示上一个项目。未清除列表以显示新项目。

这是适配器类:

public class ImagesAdapter extends RecyclerView.Adapter<ImagesAdapter.NewsViewHolder> {
private Context context;
private List<Hits> articles;

public ImagesAdapter(Context context, List<Hits> articles) {
this.context = context;
this.articles = articles;
}

@NonNull
@Override
public ImagesAdapter.NewsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.images_item, parent, false);
return new NewsViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ImagesAdapter.NewsViewHolder holder, final int position) {
String image = articles.get(position).getLargeImageURL();
Log.e("imagestest",image);
if (image != null) {
Glide.with(context) //passing context
.load(image) //passing your url to load image.
.dontAnimate()
.placeholder(context.getDrawable(R.drawable.image_not_available)) //this would be your default image (like default profile or logo etc). it would be loaded at initial time and it will replace with your loaded image once glide successfully load image using url.
//.centerCrop()//this method help to fit image into center of your ImageView
.into(holder.ivNews); //pass imageView reference to appear the image.*/
}
}

@Override
public int getItemCount() {
return articles.size();
}

class NewsViewHolder extends RecyclerView.ViewHolder {

ImageView ivNews;
private NewsViewHolder(@NonNull View itemView) {
super(itemView);
ivNews = itemView.findViewById(R.id.ivNews);

}
}
}

我不知道我到底需要在哪里清除 View 以获取新搜索的项目。

试验 1:我尝试清除列表对象articleArrayList,但它会引发适配器大小为空的崩溃。

最佳答案

清洁

articleArrayList

在再次加载搜索到的项目之前......通过执行此行articleArrayList.clear();

public void onResponse(Call<MyImages> call, Response<MyImages> response) {
Log.e("response","response"+response.code());
if(response.isSuccessful()){
Log.e("response","response"+response.code());
if((response.body() != null ? response.body().getHits() : null) !=null){
List<Hits> imagesList = response.body().getHits();
                            articleArrayList.clear();
                          articleArrayList.addAll(imagesList);
newsAdapter.notifyDataSetChanged();
}
}

关于java - 如何清除searchview中的recyclerview/adapter以进行下一次搜索android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56949706/

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