NullPointer Exception While Doing Search Feature for ListView With BaseAdapter

前端 未结 3 1776
误落风尘
误落风尘 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 20:55

    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.

    0 讨论(0)
  • 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<ApplicationInfo>)results.values;
        notifyDataSetChanged();
      }
    
    }
    
    0 讨论(0)
  • 2021-01-27 21:16

    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;
        }
    
    0 讨论(0)
提交回复
热议问题