Reputation: 73
I need to search item in listview.I have easy array of String and Array Adapter,put code wich I find at some different questions and its work!But I have one problem,this search is very slow and lags for one letter.I have word "arbuz"(one for many others) and I print a-nothing,some another letter-and words search for "a",another-and no words like this.I hope you unterstand me.How I can improve this search? My code:
public class FragmentWithList extends ListFragment {
ListView mainList;
EditText inputSearch;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_with_list, null);
mainList = (ListView) v.findViewById(android.R.id.list);
inputSearch = (EditText)v.findViewById(R.id.inputSearch);
return v;
}
public void onCreate(Bundle savedInstanceState){
setHasOptionsMenu(true);
super.onCreate(savedInstanceState);
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String values[] = new String[]{"arbuz","abrikos","banan","cup","charlie","derevo","grusha","ogurets","sliva","jabloko","volk","dom","igra","pharaon","muscle"};
Comparator<String> ALPHABETICAL_ORDER1 = new Comparator<String>() {
public int compare(String object1, String object2) {
int res = String.CASE_INSENSITIVE_ORDER.compare(object1.toString(), object2.toString());
return res;
}
};
Collections.sort(Arrays.asList(values), ALPHABETICAL_ORDER1);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,values);
mainList.setAdapter(adapter);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
adapter.getFilter().filter(s.toString());
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
}
Upvotes: 0
Views: 39
Reputation: 2260
Add the following code in the afterTextChanged()
method
adapter.getFilter().filter(s.toString());
Upvotes: 1