How to Implement Getfilter() with Custom Adapter That Extends Baseadapter

how to implement getFilter() on a BaseAdapter

Use (if you sure about data and device name are both not null)

if(data.getDeviceName().toLowerCase().startsWith(constraint.toString()))

instead of

   if(data.toLowerCase().startsWith(constraint.toString()))

In addition,
Check this likely question and answer.
Question

implement getFilter in customAdapter that extends BaseAdapter

I have found a solution and share with you, I added addTextChangedListener to inputSearch in MainActivity:

inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {

textLength = inputSearch.getText().length();
//allProd_sort and allSpec_sort are ArrayList for search
allProd_sort.clear();
allSpec_sort.clear();
String text = inputSearch.getText().toString();

//allProdString is the String get from ArrayList allProd
for (int y =0; y<allProdString.length; y++) {

//in my case the search works only if there are min 3 characters in search
if (textLength <= allProdString[y].length() && textLength >=3) {

if (Pattern.compile(Pattern.quote(text), Pattern.CASE_INSENSITIVE)
.matcher(allProdString[y]).find()) {

allProd_sort.add(allProdString[y]);
allSpec_sort.add(allSpecString[y]);
}
}
}

String[] allProdStringSort = allProd_sort.toArray(new String[allProd_sort.size()]);
String[] allSpecStringSort = allSpec_sort.toArray(new String[allSpec_sort.size()]);

listView.setAdapter(new ListViewAdapter(MainActivity.this, allProdStringSort, allSpecStringSort));
}

@Override
public void afterTextChanged(Editable editable) {

}
});

How Do Implement getFilter() with a Custom BaseAdapter?

You did not change list's data at Filter#publishResults()

in Filter#performFiltering()

ArrayList<HashMap<String, String>> Filtered_Names = new ArrayList<String>();
for(int i = 0; i<data.size(); i++){
// Filtered_Names#add()
}
Result.values = Filtered_Names;
Result.count = Filtered_Names.size();
return Result;

in Filter#publishResult()

// do something check

ArrayList<HashMap<String, String>> resultList =
(ArrayList<HashMap<String, String>>) results.values;
LazyAdapter.this.data = resultList;
LazyAdapter.this.notifyDataSetChanged();

Custom getFilter in custom ArrayAdapter in android

You are having problem, mainly because you are using custom object. If you pass a String or int value to array adapter its know how to filter it. But if you pass custom object default filter implementation have to no idea how to deal with that.

Although it is not clear what you are trying to do in your filter i recommend you following steps.

  1. Proper implementation of ListTO, although it has nothing to do with your goal right now
  2. Implement custom filter
  3. return your filter

Implement custom filter

First thing you have to do is, implements Filterable from your array adapter.

Second, provide implementation of your Filter

Filter myFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
ArrayList<ListTO> tempList=new ArrayList<ListTO>();
//constraint is the result from text you want to filter against.
//objects is your data set you will filter from
if(constraint != null && objects!=null) {
int length=objects.size();
int i=0;
while(i<length){
ListTO item=objects.get(i);
//do whatever you wanna do here
//adding result set output array

tempList.add(item);

i++;
}
//following two lines is very important
//as publish result can only take FilterResults objects
filterResults.values = tempList;
filterResults.count = tempList.size();
}
return filterResults;
}

@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence contraint, FilterResults results) {
objects = (ArrayList<ListTO>) results.values;
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
};

Last step,

@Override
public Filter getFilter() {
return myFilter;
}

Custom adapter, listview getFilter method cannot be resolved

You have wrong type of your instance when you declare facilityAdapter.
Change BaseAdapter facilityAdapter; to CustomAdapter facilityAdapter;. It should resolve your issue.



Related Topics



Leave a reply



Submit