Sid
Sid

Reputation: 21

custom filter not working in android

I have implemented custom filter in list but not working, can some body tell me where is the problem in my code ??

this is my on create method .

  @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_testsample);

    sv = (SearchView) findViewById(R.id.searchView);


    item_list = (ArrayList<Item_Category>) getLastNonConfigurationInstance();

    if (item_list == null) {
        lv = (ListView) findViewById(R.id.listView3);
        lv.setItemsCanFocus(false);
        lv.setChoiceMode(lv.CHOICE_MODE_MULTIPLE);
        item_list = new ArrayList<Item_Category>();

        item_list.add(new Item_Category("gold", false));
        item_list.add(new Item_Category("sugar", false));
        item_list.add(new Item_Category("gulli", false));
        item_list.add(new Item_Category("silver", false));
        item_list.add(new Item_Category("chmacham", false));


        arrayList = new ArrayList<Item_Category>();

        arrayList.addAll(item_list);
        arrayAdapter = new ItemCategoryArrayAdapter(this, arrayList);

        lv.setAdapter(arrayAdapter);
    }

    sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {

            return false;
        }

        @Override
        public boolean onQueryTextChange(String text) {



            selected = text.trim();

            //lv = (ListView)findViewById(R.id.listView);
            if (arrayAdapter != null) {
                arrayAdapter.getFilter().filter(selected);
                lv.setAdapter(arrayAdapter);
                return true;
            }
            return true;
        }
    });

}

this is all for custom list view and for custom filter..

  private class Item_Category
{

    String category_Name ;
    boolean checked ;

    public String getCategory_Name() {
        return category_Name;
    }

    public void setCategory_Name(String category_Name) {
        this.category_Name = category_Name;
    }

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }

    public Item_Category(String category_Name, boolean checked) {
        this.category_Name = category_Name;
        this.checked = checked;
    }



}

private static class Item_Category_ViewHolder
{
    TextView categoryName;
    CheckBox checkBox;

    public TextView getCategoryName() {
        return categoryName;
    }

    public void setCategoryName(TextView categoryName) {
        this.categoryName = categoryName;
    }

    public CheckBox getCheckBox() {
        return checkBox;
    }

    public void setCheckBox(CheckBox checkBox) {
        this.checkBox = checkBox;
    }


    public Item_Category_ViewHolder(TextView categoryName, CheckBox checkBox) {
        this.categoryName = categoryName;
        this.checkBox = checkBox;
    }
}


public class ItemCategoryArrayAdapter extends ArrayAdapter<Item_Category> implements Filterable {
    private LayoutInflater inflater;
    ArrayList<String> itemList;
    List<Item_Category> filtered;
    List<Item_Category> mStringFilterList;
    ItemCategoryFilter categoryFilter;

    public ItemCategoryArrayAdapter(Context context, List<Item_Category> categoryList) {
        super(context, R.layout.everyday_item_category_row, R.id.itemName, categoryList);
        // Cache the LayoutInflate to avoid asking for a new one each time.

        filtered = new ArrayList<Item_Category>(categoryList);
        this.inflater = LayoutInflater.from(context);
        mStringFilterList = new ArrayList<Item_Category>(categoryList);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Planet to display
        Item_Category item_list = (Item_Category) this.getItem(position);

        // The child views in each row.
        CheckBox checkBox;
        TextView itemCategory;

        // Create a new row view
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.everyday_item_category_row, null);

            // Find the child views.
            itemCategory = (TextView) convertView.findViewById(R.id.itemName);
            checkBox = (CheckBox) convertView.findViewById(R.id.CheckBox);

            // Optimization: Tag the row with it's child views, so we don't have to
            // call findViewById() later when we reuse the row.
            convertView.setTag(new Item_Category_ViewHolder(itemCategory, checkBox));

            // If CheckBox is toggled, update the planet it is tagged with.
            checkBox.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    CheckBox cb = (CheckBox) v;
                    Item_Category item_list1 = (Item_Category) cb.getTag();
                    item_list1.setChecked(cb.isChecked());
                }
            });
        }
        // Reuse existing row view
        else {
            // Because we use a ViewHolder, we avoid having to call findViewById().
            Item_Category_ViewHolder viewHolder = (Item_Category_ViewHolder) convertView.getTag();
            checkBox = viewHolder.getCheckBox();
            itemCategory = viewHolder.getCategoryName();

        }

        // Tag the CheckBox with the Planet it is displaying, so that we can
        // access the planet in onClick() when the CheckBox is toggled.
        checkBox.setTag(item_list);

        // Display planet data
        checkBox.setChecked(item_list.isChecked());
        itemCategory.setText(item_list.getCategory_Name());

        CheckBox c1 = (CheckBox) convertView.findViewById(R.id.CheckBox);
        final TextView t1 = (TextView) convertView.findViewById(R.id.itemName);
        c1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {


                if (isChecked) {
                    itemList.add(t1.getText().toString());
                } else {
                    if (itemList.contains(t1.getText().toString())) {
                        itemList.remove(t1.getText().toString());
                    }

                }
            }
        });


        return convertView;
    }
    @Override
    public Filter getFilter() {
        if (categoryFilter == null)
            categoryFilter = new ItemCategoryFilter();

        return categoryFilter;
    }


    private class ItemCategoryFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            mStringFilterList = new ArrayList<Item_Category>();
            FilterResults results = new FilterResults();

            if (constraint != null && constraint.length() > 0) {
                ArrayList<Item_Category> filterList = new ArrayList<Item_Category>();
                for (int i = 0; i < mStringFilterList.size(); i++) {
                    if ((mStringFilterList.get(i).getCategory_Name().toUpperCase())
                            .contains(constraint.toString().toUpperCase())) {

                        Item_Category itemCategory = new Item_Category(mStringFilterList.get(i)
                                .getCategory_Name(), false);

                        filterList.add(itemCategory);
                    }
                }
                results.count = filterList.size();
                results.values = filterList;
            } else {
                results.count = mStringFilterList.size();
                results.values = mStringFilterList;
            }
            return results;
        }

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

            // Now we have to inform the adapter about the new list filtered
            filtered = (ArrayList<Item_Category>) results.values;
            notifyDataSetChanged();
        }

    }

Upvotes: 0

Views: 353

Answers (2)

Adam Collins Razor
Adam Collins Razor

Reputation: 11

Creating an Adapter to a Custom grid view or list view, both have some problems in filtering. You see that, the adapters won't filter the data in your grid or list views.

I can't paste code, but I would recommend you to take a look at implementation of ArrayAdapter.class. Make sure that ItemCategoryArrayAdapter.class has most of the methods present in ArrayAdapter.class.

Just make sure most of the methods present in ArrayAdapter.class are implemented in ItemCategoryArrayAdapter.class.

This will fix it...

Upvotes: 1

Ragesh Ramesh
Ragesh Ramesh

Reputation: 3520

Change your adapter constructor as follows.

    public ItemCategoryArrayAdapter(Context context, List<Item_Category> categoryList) {
     filtered = new ArrayList<Item_Category>(categoryList);
    super(context, R.layout.everyday_item_category_row, R.id.itemName, filtered);
    // Cache the LayoutInflate to avoid asking for a new one each time.

You are passing the complete list ie categoryList instead of filtered List and hence it is not reflecting.

Upvotes: 0

Related Questions