There are more than 2,000 items on my listview and no matter which item I search for, after pressing a letter, listView goes blank like as if there were no relevant items.
I'm guessing it's beacuse I'm using the simple_list_item_2 layout which is provided by Android Studio. It has two textView fields on it and that is why filtering may be messed up. It's just a guess of mine though.
Here's the adapter and 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);
}
});
And here's the searchView:
customersSearchView.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
});
If you're using a ContentProvider to retrieve your data list, then the following line in onQueryTextChange()
will not work:
adapter.getFilter().filter(newText);
If you are using a CursorLoader (with the ContentProvider), you must filter the data using the LoaderManager callbacks, not using the adapter directly. You would need to change your onQueryTextChange()
method to something like this:
private String cursorFilter;
@Override
public boolean onQueryTextChange(String newText) {
cursorFilter = !TextUtils.isEmpty(newText) ? newText : null;
getLoaderManager().restartLoader(0, null, this);
return true;
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
Uri baseUri;
if (cursorFilter != null) {
// Filter the data with the cursorFilter
baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI,
Uri.encode(cursorFilter));
} else {
baseUri = MyContentProvider.CONTENT_URI;
}
...
}
See here for an example: http://android-er.blogspot.co.uk/2013/02/query-contacts-database-using-loader.html