0

I am trying to search from CustomListView implementing Filterable in BaseAdapter, but it provides repeated search data..

            @Override
    public Filter getFilter() {
        // TODO Auto-generated method stub
         // Here myList contains Original data
        return new MyFilter(myList);
    }

private class MyFilter extends Filter {

    List<ListModel> myValues;

    public MyFilter(List<ListModel> list) {
        // TODO Auto-generated constructor stub
        myValues = new ArrayList<ListModel>(list);
    }

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        // TODO Auto-generated method stub

        FilterResults results = new FilterResults();

        List<ListModel> temp = new ArrayList<ListModel>();

        if (TextUtils.isEmpty(constraint)) {

            if (temp.size() > 0)
                temp.clear();
            temp.addAll(myValues);
        }

        else {
            for (ListModel model : myValues) {

                if (model.getNames().toLowerCase.contains(constraint.toString().toLowerCase()))
                    temp.add(model);
            }

        }
        results.values = temp;
        results.count = temp.size();

        return results;
    }

      @SuppressWarnings("unchecked")
      @Override
    protected void publishResults(CharSequence constraint,
            FilterResults results) {
        // TODO Auto-generated method stub

        if (results != null && results.count > 0) {

            myList.clear();
            myList.addAll((List<ListModel>) results.values);

        } else
            myList.addAll(myValues);

        mAdapter.notifyDataSetChanged();

    }
}

I am not getting where i made a mistake, search works perfectly but it adds more data to my custom list view..

Example

suppose i search "India", then it adds whole list view data as "India".If my list view holds the size of 10 rows, it makes 20 rows with "India"

Any Help would be Appreciated.

moDev
  • 5,248
  • 4
  • 33
  • 63

1 Answers1

1

Finally i am able to solve my own question..

Here are few things that solved my problem..

    @Override
    public Filter getFilter() {
        // TODO Auto-generated method stub

        return new MyFilter();
    }

    private class MyFilter extends Filter {

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        // TODO Auto-generated method stub

        FilterResults results = new FilterResults();

        List<ListModel> temp = new ArrayList<ListModel>();

        if (TextUtils.isEmpty(constraint)) {

            if (temp.size() > 0)
                temp.clear();
            // I have created new List from old myList before search begins
            temp.addAll(newList);
        }
        else
        {
            // newList 
            for (ListModel model : newList)
            {
                if(model.getNames().toLowerCase.contains(constraint.toString().toLowerCase()))
                    temp.add(model);
            }

        }
        results.values = temp;
        results.count = temp.size();

        return results;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint,
            FilterResults results) {
        // TODO Auto-generated method stub

              myList.clear();

        if (results != null && results.count > 0)         
            myList.addAll((List<ListModel>) results.values);
        else
            myList.addAll(myList);

        mAdapter.notifyDataSetChanged();

    }
} //updated code
Zong
  • 6,160
  • 5
  • 32
  • 46
moDev
  • 5,248
  • 4
  • 33
  • 63
  • Do you happen to have the full code? I am having an issue: http://stackoverflow.com/questions/20524417/listview-is-blank-while-using-getfilter-function – Si8 Dec 13 '13 at 16:40