0

I implemented the search functionality using edit text.so in onTextChange Listener I called the get filter method. my adapter is extending from the base adapter class not from the Array adapter class. So in my adapter class, I created the filter method and call that filter method in the ontextChange listener. When I entered the text in the search bar my app getting crash because of this error: Attempt to invoke virtual method 'void android.widget.Filter.filter(java.lang.CharSequence)' on a null object reference. This my adapter class:

public class Splitadapter extends BaseAdapter implements Filterable{
    /*
    * Variables Declaration section
    *
    *
    */
    private ArrayList<COntactsModel> _Contacts;
    private Context mContext;
    private LayoutInflater inflater;
    private ValueFilter valueFilter;
    private ArrayList<COntactsModel> mStringFilterList;

    public Splitadapter(Context context,ArrayList<COntactsModel> _Contacts){
        super();
        mContext = context;
        this._Contacts = _Contacts;
        mStringFilterList =  _Contacts;
        this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        getFilter();

    }//End of CustomAdapter constructor

    public int getCount(){
        return _Contacts.size();
    }//End of getCount method

    public Object getItem(int position){
        return _Contacts.get(position).getName();
    }//End of getItem method

    public long getItemId(int position){
        return position;
    }//End of getItemId method

    public class ViewHolder{
        TextView textviewName;
        TextView textviewNumber;
        CheckBox checkbox;
        ImageView image;
        int id;
    }

    public View getView(int position, View convertView, ViewGroup parent){
        ViewHolder holder;
        final int pos = position;

        if (convertView == null){
            holder = new ViewHolder();

            convertView = LayoutInflater.from(mContext).inflate(R.layout.list, null);
            holder.textviewName = (TextView) convertView.findViewById(R.id.name);
            holder.textviewNumber = (TextView) convertView.findViewById(R.id.mobile);
            holder.checkbox = (CheckBox) convertView.findViewById(R.id.check);
            holder.image = convertView.findViewById(R.id.image);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }//End of else

        holder.checkbox.setId(position);
        holder.textviewName.setId(position);
        holder.textviewNumber.setId(position);


        holder.textviewName.setText(_Contacts.get(position).getName());
        holder.textviewNumber.setText(_Contacts.get(position).getPhonenum());

        holder.id = position;

        return convertView;
    }//End of getView method

    @Override
    public Filter getFilter() {
        if(valueFilter==null) {
            valueFilter=new ValueFilter();
        }
        return null;
    }
    private class ValueFilter extends Filter {

        //Invoked in a worker thread to filter the data according to the constraint.
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results=new FilterResults();
            if(constraint!=null && constraint.length()>0){
                ArrayList<COntactsModel> filterList=new ArrayList<COntactsModel>();
                for(int i=0;i<mStringFilterList.size();i++){
                    if((mStringFilterList.get(i).getName().toUpperCase())
                    .contains(constraint.toString().toUpperCase())) {
                        COntactsModel contacts = new COntactsModel();
                        contacts.setName(mStringFilterList.get(i).getName());
                        contacts.setPhonenum(mStringFilterList.get(i).getPhonenum());
                        filterList.add(contacts);
                    }
                }
                results.count=filterList.size();
                results.values=filterList;
            }else{
                results.count=mStringFilterList.size();
                results.values=mStringFilterList;
            }
            return results;
        }


        //Invoked in the UI thread to publish the filtering results in the user interface.
        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,
                  FilterResults results) {
            _Contacts=(ArrayList<COntactsModel>) results.values;
            notifyDataSetChanged();
        }
    }
    //End of CustomAdapter instance inner class


}

And this is my items class :

public class COntactsModel
{
    String phonenum;
    String cname;
    public String getPhonenum() {
        return phonenum;
    }
    public void setPhonenum(String phonenum) {
        this.phonenum = phonenum;
        System.out.println("se ph num"+phonenum);
    }
    public String getName() {
        return cname;
    }
    public void setName(String name) {
        this.cname = name;
    }
}

How to resolve that one?

Morteza Jalambadani
  • 2,190
  • 6
  • 21
  • 35
Vijaya
  • 21
  • 5

1 Answers1

1
@Override
    public Filter getFilter() {
        if(valueFilter==null) {

            valueFilter=new ValueFilter();

        }

        return null;
    }

replace with

    @Override
    public Filter getFilter() {
        if(valueFilter==null) {

            valueFilter=new ValueFilter();

        }

        return valueFilter;
    }
Rahul
  • 4,699
  • 5
  • 26
  • 38