I just finished adding a search feature to my android widget to search through a list of the users installed applications. My app installs fine and everything but when I go to s
Seems that mListAppInfo
is null in
@Override
public int getCount() {
return mListAppInfo.size();
}
Which makes me suppose that for some reason your call to getInstalledApplications
when you instantiate the adapter returns null.
Your list contains appInfo.packageName
not appInfo.name
.
In performFiltering method you are adding appInfo.name
to filtered list. Instead you should add appInfo.packageName
.
Your for loop should be like this
for (ApplicationInfo appInfo : originalListAppInfo) {
String somefield = appInfo.packageName;
if (somefield.toLowerCase().contains(constraint.toString())) {
myFilteredAppList.add(appInfo);
}
}
This should work.
And never forget to check for null values in publishResults
method
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if(results.values != null)
{
mListAppInfo = (List<ApplicationInfo>)results.values;
notifyDataSetChanged();
}
}
Add this code before for loop:
if (constraint.length() == 0) {
myFilteredAppList.addAll(originalListAppInfo);
}
i.e
@Override
public Filter getFilter() {
if(filter == null) {
filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
List<ApplicationInfo> myFilteredAppList = new ArrayList<ApplicationInfo>();
constraint = constraint.toString().toLowerCase();
if (constraint.length() == 0) {
myFilteredAppList.addAll(originalListAppInfo);
}
for (ApplicationInfo appInfo : originalListAppInfo) {
String somefield = appInfo.name;
if (somefield.toLowerCase().contains(constraint.toString())) {
myFilteredAppList.add(appInfo);
}
}
results.count = myFilteredAppList.size();
results.values = myFilteredAppList;
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
mListAppInfo = (List<ApplicationInfo>)results.values;
notifyDataSetChanged();
}
};
}
return filter;
}