我正在使用翻新来搜索图像并显示。我正在使用SearchView进行搜索,recyclerview和适配器来显示该项目。
这是第一次工作。第二次搜索时,它显示的是同一项目。它不会清除要显示的视图。
这是我的代码:
document.querySelectorAll('[name^="dropDown"]').forEach(function(node) {console.log(node.options[node.selectedIndex].value);});
在这里我需要重置以搜索新项目,以避免显示上一个项目。列表未清除以显示新项目。
这是适配器类:
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();
}
}
我不知道要在何处清除视图以获取新搜索的项目。
试验1:我试图清除列表对象articleArrayList,但适配器大小为null时会引发崩溃。
答案 0 :(得分:0)
清洁
articleArrayList
在再次加载搜索到的项目之前。...通过执行此行"New Value"
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();
答案 1 :(得分:0)
您要将生成的imagesList
添加到articleArrayList
,您应该替换这些值。
articleArrayList = imagesList;