I have implemented filterable in customadapter class and i am able to search through listview ,but when i empty my searchbox my list still show the last search app and not with whole app list.
mainactivity.java
public class MainActivity extends ListActivity {
List<ApplicationInfo> appList;
ListAdapter listAdapter;
PackageManager packageManager;
Process suProcess;
DataOutputStream os;
ApplicationInfo info;
EditText search;
ArrayList<ApplicationInfo> applist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
packageManager = getPackageManager();
new LoadApplication().execute();
search = (EditText) findViewById(R.id.searchView);
search.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
listAdapter.getFilter().filter(s.toString());
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
}
I have added my Filter Class.
private class ListFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint == null || constraint.length() == 0) {
results.values = applist;
results.count = applist.size();
} else {
ArrayList<ApplicationInfo> info = new ArrayList<ApplicationInfo>();
for (ApplicationInfo c : applist) {
if (c.loadLabel(packageManager).toString().toUpperCase().contains(constraint.toString().toUpperCase())) {
info.add(c);
}
}
results.values = info;
results.count = info.size();
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results.count == 0) {
notifyDataSetInvalidated();
} else {
applist = (List<ApplicationInfo>) results.values;
notifyDataSetChanged();
}
}
}
And overrided getFilter() method.
public Filter getFilter() {
if (filter == null)
{
filter=new ListFilter();
}
return filter;
}