adapter.getFilter().filter(newText) 在 Activity 中不起作用,但在 Fragment 中正常工作
Fragment_Adapter_Calling.java 文件,
MyStudentAdapterU adp = new MyStudentAdapterU(stuData, getContext());
stdRecyclerView.setAdapter(adp);
stdRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
inflater.inflate(R.menu.search_menu,menu);
MenuItem item=menu.findItem(R.id.search_menu);
SearchView searchView=(SearchView)item.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
adp.getFilter().filter(newText);
return false;
}
});
super.onCreateOptionsMenu(menu, inflater);
}
但当我尝试在 Activity.java 文件中粘贴相同的代码来调用适配器时,它显示错误“无法解析 'ProviderAdapter' 中的方法 'getFilter' adapter.getFilter.filter(newText) 行;
ProviderAdapter adp=new ProviderAdapter(ProviderActivityList.this,lists);
r1.setAdapter(adp);
r1.setLayoutManager(new LinearLayoutManager(ProviderActivityList.this));
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_menu,menu);
MenuItem item=menu.findItem(R.id.search_menu);
SearchView searchView=(SearchView)item.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
adp.getFilter().filter(newText);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
错误图片 错误屏幕截图
Fragment_Adapter_Calling.java file
MyStudentAdapterU adp = new MyStudentAdapterU(stuData, getContext());
stdRecyclerView.setAdapter(adp);
stdRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
inflater.inflate(R.menu.search_menu,menu);
MenuItem item=menu.findItem(R.id.search_menu);
SearchView searchView=(SearchView)item.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
adp.getFilter().filter(newText);
return false;
}
});
super.onCreateOptionsMenu(menu, inflater);
}
but when I try to paste the same code in Activity.java file to call adapter, It shows an error "Cannot resolve method 'getFilter' in 'ProviderAdapter' on
adapter.getFilter.filter(newText) line;
ProviderAdapter adp=new ProviderAdapter(ProviderActivityList.this,lists);
r1.setAdapter(adp);
r1.setLayoutManager(new LinearLayoutManager(ProviderActivityList.this));
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_menu,menu);
MenuItem item=menu.findItem(R.id.search_menu);
SearchView searchView=(SearchView)item.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
adp.getFilter().filter(newText);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
Error picture
Error Screenshot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在第一个代码片段中,您将
adp
设置为MyStudentAdapterU
,它显然有一个getFilter()
方法。在第二个代码片段中,您将
adp
设置为ProviderAdapter
,这显然没有有getFilter()
方法。In the first code snippet, you set
adp
toMyStudentAdapterU
which obviously has agetFilter()
method.In the second code snipper, you set
adp
toProviderAdapter
which obviously does NOT have agetFilter()
method.