Custom getFilter in custom ArrayAdapter in android

No need to write array adapter. write a toString() method which should return the value of filename.

Like

public class ListTO {

    public int Id;
    public String FileName;
    public String FileUri;

    public ListTO(int id, String fileName, String fileUri) {

        Id = id;
        FileName = fileName;
        FileUri = fileUri;

    }

    public String toString(){
        return FileName
    }

}

try this:

     public class Adptr extends BaseAdapter implements Filterable {
public ArrayList<Model> modelValues;

private Activity activity;
private LayoutInflater layoutinflater;
private List<Model> mOriginalValues;
private int PositionSelected = 0;

public Adptr (ArrayList<Model> modelValues, Activity activity) {
    super();
    this.modelValues = modelValues;
    this.activity = activity;


}

@Override
public int getCount() {

    return modelValues.size();
}

@Override
public Object getItem(int position) {

    return modelValues.get(position);
}

@Override
public long getItemId(int position) {

    return position;
}


@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    layoutinflater = (LayoutInflater)  activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ViewHolder holder = null;
    Model model = modelValues.get(position);

    if (convertView == null || !(convertView.getTag() instanceof ViewHolder)) {
        convertView = layoutinflater.inflate(R.layout.row_search, null);
        holder = new ViewHolder();
        holder.txtName = (TextView) convertView.findViewById(R.id.row_serch_txt_name);




        convertView.setTag(holder);
        convertView.setTag(R.id.row_serch_txt_name, holder.txtName);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.txtArtistName.setText("" + modelValue.get_NAME());




    return convertView;
}

class ViewHolder {
    TextView txtName;


}

@Override
public Filter getFilter() {

    Filter filter = new Filter() {

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {

            modelValues = (ArrayList<ModelValueArtist>) results.values; // has

            notifyDataSetChanged();
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults(); // Holds the
                                                            // results of a
                                                            // filtering
                                                            // operation in
                                                            // values
            // List<String> FilteredArrList = new ArrayList<String>();
            List<Model> FilteredArrList = new ArrayList<Model>();

            if (mOriginalValues == null) {
                mOriginalValues = new ArrayList<Model>(modelValues); // saves

            }

            /********
             * 
             * If constraint(CharSequence that is received) is null returns
             * the mOriginalValues(Original) values else does the Filtering
             * and returns FilteredArrList(Filtered)
             * 
             ********/
            if (constraint == null || constraint.length() == 0) {

                // set the Original result to return
                results.count = mOriginalValues.size();
                results.values = mOriginalValues;
            } else {
                Locale locale = Locale.getDefault();
                constraint = constraint.toString().toLowerCase(locale);
                for (int i = 0; i < mOriginalValues.size(); i++) {
                    Model model = mOriginalValues.get(i);

                    String data = model.get_NAME();
                    if (data.toLowerCase(locale).contains(constraint.toString())) {

                        FilteredArrList.add(modelMyMall);
                    }
                }
                // set the Filtered result to return
                results.count = FilteredArrList.size();
                results.values = FilteredArrList;

            }
            return results;
        }
    };
    return filter;
  }

     }

You need to override the getFilter() method in the Adapter and provide your own filter. Take a look in this Filterable Example to see an actual implementation.

Add the following getFilter() code to your FilterableAdapter class and fill it with your filtering:

/* (non-Javadoc)
 * @see android.widget.ArrayAdapter#getFilter()
 */
@Override
public Filter getFilter() {
    return new Filter() {

        /* (non-Javadoc)
         * @see android.widget.Filter#performFiltering(java.lang.CharSequence)
         */
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            // TODO Auto-generated method stub
            /*
             * Here, you take the constraint and let it run against the array
             * You return the result in the object of FilterResults in a form
             * you can read later in publichResults.
             */
            return null;
        }

        /* (non-Javadoc)
         * @see android.widget.Filter#publishResults(java.lang.CharSequence, android.widget.Filter.FilterResults)
         */
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            // TODO Auto-generated method stub
            /*
             * Here, you take the result, put it into Adapters array
             * and inform about the the change in data.
             */
        }

    };
}

I've added hints what to do in the comments.


You are having problem, mainly because you are using custom object. If you pass a String or int value to array adapter its know how to filter it. But if you pass custom object default filter implementation have to no idea how to deal with that.

Although it is not clear what you are trying to do in your filter i recommend you following steps.

  1. Proper implementation of ListTO, although it has nothing to do with your goal right now
  2. Implement custom filter
  3. return your filter

Implement custom filter

First thing you have to do is, implements Filterable from your array adapter.

Second, provide implementation of your Filter

Filter myFilter = new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
         FilterResults filterResults = new FilterResults();   
         ArrayList<ListTO> tempList=new ArrayList<ListTO>();
         //constraint is the result from text you want to filter against. 
         //objects is your data set you will filter from
         if(constraint != null && objects!=null) {
             int length=objects.size();
             int i=0;
                while(i<length){
                    ListTO item=objects.get(i);
                    //do whatever you wanna do here
                    //adding result set output array     

                    tempList.add(item);

                    i++;
                }
                //following two lines is very important
                //as publish result can only take FilterResults objects
                filterResults.values = tempList;
                filterResults.count = tempList.size();
          }
          return filterResults;
      }

      @SuppressWarnings("unchecked")
      @Override
      protected void publishResults(CharSequence contraint, FilterResults results) {
          objects = (ArrayList<ListTO>) results.values;
          if (results.count > 0) {
           notifyDataSetChanged();
          } else {
              notifyDataSetInvalidated();
          }  
      }
     };

Last step,

@Override
     public Filter getFilter() {
        return myFilter;
    }