我的列表视图中有超过2000个项目,无论按哪个项目,按下一个字母后,listView都会变成空白,就像if没有相关的项目。尝试使用SearchView过滤ListView。即使ListView中存在相关项目,也不会显示任何项目,即使ListView上存在相关项目。
我猜这是因为我使用Android Studio提供的simple_list_item_2布局。它有两个textView字段,这就是为什么过滤可能会搞砸。这只是我的猜测。
这里的适配器和ListView:
adapter = new ArrayAdapter(ViewExistingCustomersActivity.this, android.R.layout.simple_list_item_2, android.R.id.text1, customers) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView text1 = (TextView) view.findViewById(android.R.id.text1);
TextView text2 = (TextView) view.findViewById(android.R.id.text2);
text1.setText(customers.get(position).getDefinition());
text2.setText(customers.get(position).getAddress1() + " Bakiye: " + customers.get(position).getBalance());
return view;
}
};
customersListView.setAdapter(adapter);
// Dokunulan ListView elemanının detaylarını gösterecek ekranı çağıran kod.
customersListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent viewCustomerDetailsIntent = new Intent(ViewExistingCustomersActivity.this, CurrentStatementActivity.class);
viewCustomerDetailsIntent.putExtra("customer", customers.get(i));
viewCustomerDetailsIntent.putExtra("user", user);
startActivity(viewCustomerDetailsIntent);
}
});
而这里的搜索查看:
customersSearchView.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
});
【如何过滤与搜索查看一个RecyclerView(HTTPS的可能重复://计算器。 com/questions/30398247/how-to-filter-a-recyclerview-with-a-searchview) –