Skip to content Skip to sidebar Skip to footer

Nullpointerexception At Lazyadapter

I am trying to perform a search on LazyAdapter class by creating a custom Filter. But when I am trying to perform the search by using the TextWatcher, the application is forcing cl

Solution 1:

To retain data items after you clear search edittext, below code is useful. Hope this helps you.

public Filter getFilter() 
{
    Filter filter = new Filter() 
    {           
        @SuppressWarnings("unchecked")
        @Override
        protectedvoidpublishResults(CharSequence constraint, FilterResults results) 
        {
            System.out.println(" --- constraint in publishResults --- "+constraint+" - results - "+results);
            itemDetailList = (List<Dish>)results.values;
            notifyDataSetChanged();
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) 
        {
            FilterResults filterResults = new FilterResults();
            List<Dish> filtered_list = new ArrayList<Dish>();
            if(originalItemsDetails == null)
            {
                System.out.println(" --- originalItemsDetails IS NULL --- ");
                originalItemsDetails = new ArrayList<Dish>(itemDetailList);
            }
            else
            {
                System.out.println(" --- originalItemsDetails NOT NULL --- ");
            }

            if(constraint == null || constraint.length() == 0) 
            {
                filterResults.count = originalItemsDetails.size();
                filterResults.values = originalItemsDetails;
            } 
            else 
            {
                constraint = constraint.toString().toLowerCase();
                for(int i=0; i<originalItemsDetails.size(); i++) 
                {
                    String dataNames = originalItemsDetails.get(i).getDishName().toLowerCase();
                    if(dataNames.startsWith(constraint.toString()))  
                    {
                        filtered_list.add(originalItemsDetails.get(i));
                    }
                }

                filterResults.count = filtered_list.size();
                System.out.println(" --- filterResults.count --- "+filterResults.count);

                filterResults.values = filtered_list;
                System.out.println(" --- filterResults.values --- "+filterResults.values);
            }

            System.out.println(" --- constraint in performFiltering --- "+constraint);

            return filterResults;
        }
    };

    return filter;
}

Solution 2:

Are you sure your name_org and s variables are not null???

You have to first check it, like,

if(name_org != null && s != null)
{
 if(name_org.contains(s))
 {
 // Your other stuff goes here...
 .
 .
 }
}

Solution 3:

I think s is null, have you checked it. Because contains method throws NPE even if charsequence that you pass is null

Post a Comment for "Nullpointerexception At Lazyadapter"