NullPointer Exception While Doing Search Feature for ListView With BaseAdapter

前端 未结 3 1781
误落风尘
误落风尘 2021-01-27 20:32

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

3条回答
  •  执念已碎
    2021-01-27 21:07

    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)results.values;
        notifyDataSetChanged();
      }
    
    }
    

提交回复
热议问题