Reputation: 43
now i am making a program in android using edittext and listview. I want to search the listview item using edittext above. After populate data to listview, when user type text in edittext, the listview will scroll to the position start with that text. Example: i have item: apple, application, book, boy, car, cat, cash..... when i type b in edittext then listview will scroll to book. I use the listview.setSelection(position), but because the amount of my data is over 30,000 , so when i use the following code it is slow to find data. Are there any solutions or any other method to do this? Here is my code:
YOUR_EDITTEXT.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
//LOGIC MAY DIFFER ACCORDING TO YOUR REQUIREMENT..
int POSITION = 0;
for(int i =0;i<list.size();i++) {
if(list.get(i).startsWith(s.toString()))
{
POSITION = i;
break;
}
}
listview.smoothScrollToPosition(POSITION);
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
});
Upvotes: 1
Views: 238
Reputation: 2506
You could take a different approach and load your list data with an AutoCompleteTextView. This would provide your required functionality and it's designed to handle large data sets.
Example: https://developers.google.com/places/training/autocomplete-android
Upvotes: 1
Reputation: 307
Is your list sorted? If it is, you can use binary sort on the datasource.
Upvotes: 0