I have a list view that uses a List Adapter as the adapter to populate the list view. I researched online for a way to add a search for this, but all of them are using ArrayAdapters. Since I'm using a List Adapter, the function getFilter()
doesn't work. This is my code
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
adapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
Can someone help me find an alternative way so doing this in a simple way?
Create a function in your Adapter like this:
public void filter(String charText) { charText = charText.toLowerCase(Locale.getDefault()); objectList.clear(); if (charText.length() == 0) { objectList.addAll(objectAsArraylist); } else { for (Object v : objectAsArraylist) { objectList.add(v); } } notifyDataSetChanged(); }
where objectList
is the list you passed to the adapter, and objectAsArraylist
is the objectList
passed to an arraylist.
Then you can call this method on your onTextChanged
:
adapter.filter(text)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.