I am trying to implement a getFilter() on a base adapter to filter out search results on a List. Is there any example of how to implement a getFilter()?
MainActivity.java
final AppInfoAdapter adapter = new AppInfoAdapter(this, Utilities.getSystemFilteredApplication(this), getPackageManager());
public void onTextChanged(CharSequence s, int start, int before,
int count) {
adapter.getFilter().filter(s); //Filter from my adapter
adapter.notifyDataSetChanged(); //Update my view
}
AppInfoAdapter.java
package com.example.permission;
import java.util.List;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;
public class AppInfoAdapter extends BaseAdapter implements Filterable{
private Context mContext;
private List mListAppInfo;
PackageManager mPackManager;
public AppInfoAdapter(Context c, List list, PackageManager pm) {
mContext = c;
mListAppInfo = list;
mPackManager = pm;
}
public int getCount() {
return mListAppInfo.size();
}
public Object getItem(int position) {
return mListAppInfo.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// get the selected entry
ApplicationInfo entry = (ApplicationInfo) mListAppInfo.get(position);
// reference to convertView
View v = convertView;
// inflate new layout if null
if(v == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
v = inflater.inflate(R.layout.layout_appinfo, null);
}
// load controls from layout resources
ImageView ivAppIcon = (ImageView)v.findViewById(R.id.ivIcon);
TextView tvAppName = (TextView)v.findViewById(R.id.tvName);
TextView tvPkgName = (TextView)v.findViewById(R.id.tvPack);
// set data to display
ivAppIcon.setImageDrawable(entry.loadIcon(mPackManager));
tvAppName.setText(entry.loadLabel(mPackManager));
tvPkgName.setText(entry.packageName);
// return view
return v;
}
public Filter getFilter() {
// TODO Auto-generated method stub
return null;
}
}
EDIT: Edited the code and added full AppInfoAdapter.java
You need to return an instance of a
Filter
. To write a filter, subclassFilter
and implementperformFiltering
andpublishResults
. See the docs.General procedure
Step 1:
listview.setTextFilterEnabled(true);
Step 2:
Step 3:
}
Step 4: I am not sure what type your list is, so assuming a list of String:
extend your class with ArrayAdapter,then override methods,and create object of filter class and return with it.
Can you post your full
AppInfoAdapter
? Also is there any reason extending fromBaseAdapter
and notArrayAdapter
? If you have anArrayList
of objects, useArrayAdapter
, it already implementsFilterable
interface.Actually you are using a
List
, your adapter can be rewritten to extendsArrayAdapter
which already isFilterable
.getFilter() can be override in adapters and return the filter object which contains filtered list . There are two key methods in Filter() class; performFiltering and publishResults. The first method performs the filtering in worker thread and the later one return filtered list of objects.
You can refer to sample code below
Hope you find it useful .
this almost got me killed :)
define an ArrayList of List in your public adapter class which is gonna contain temporary items of your Original List.
create getFilter() method with the following code[as an example]:
And finally in your activity for your EditText: