我的列表視圖中有超過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) –