本文介绍了自定义过滤器不能在android中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在列表中实现了自定义过滤器,但不工作,可以告诉我在我的代码中的问题在哪里?



这是我的创建方法。

  @Override 
保护无效的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){
v =(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){

返回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;
}
});






这是所有自定义列表视图和自定义过滤器。 。

  private class Item_Category 
{

String category_Name;
布尔值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;

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


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





$ b私有静态类Item_Category_ViewHolder
{
TextView categoryName;
CheckBox checkBox;

public TextView getCategoryName(){
return categoryName;
}

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

$ b $ public CheckBox getCheckBox(){
return checkBox;
}

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


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


public class ItemCategoryArrayAdapter extends ArrayAdapter< Item_Category>实现可过滤{
私人LayoutInflater inflater;
ArrayList< String> itemList中;
列表< Item_Category>过滤;
列表< 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);
//缓存LayoutInflate以避免每次都要求一个新的。

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

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

//每行中的子视图。
CheckBox checkBox;
TextView itemCategory;

//创建一个新的行视图
if(convertView == null){
convertView = inflater.inflate(R.layout.everyday_item_category_row,null);

//找到子视图。
itemCategory =(TextView)convertView.findViewById(R.id.itemName);
checkBox =(CheckBox)convertView.findViewById(R.id.CheckBox);

//优化:使用它的子视图标记行,所以我们不需要
//稍后在重用行时调用findViewById()。
convertView.setTag(new Item_Category_ViewHolder(itemCategory,checkBox));

//如果CheckBox被切换,更新它被标记的星球。
checkBox.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){$ b $ CheckBox cb =(CheckBox)v;
Item_Category item_list1 =(Item_Category)cb .getTag();
item_list1.setChecked(cb.isChecked());
}
});
}
//重复使用现有的行视图
else {
//因为我们使用ViewHolder,所以我们不必调用findViewById()。
Item_Category_ViewHolder viewHolder =(Item_Category_ViewHolder)convertView.getTag();
checkBox = viewHolder.getCheckBox();
itemCategory = viewHolder.getCategoryName();



//将CheckBox与正在显示的行星进行标记,以便我们可以
//在复选框处于onClick()切换。
checkBox.setTag(item_list);

//显示地球数据
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 $ b $ public void onCheckedChanged(CompoundButton buttonView,boolean isChecked){


if(如果(itemList.contains(t1.getText()。toString())){
itemList.add(t1.getText()。toString());
} else { {
itemList.remove(t1.getText()。toString());
}

}
}
});


返回convertView;

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

return categoryFilter;


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

mStringFilterList = new ArrayList< Item_Category>();
FilterResults results = new FilterResults(); (约束!= null&&约束。长度()> 0){
ArrayList< Item_Category>

if 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;
}
返回结果;

$ b @Override
protected void publishResults(CharSequence约束,FilterResults结果){

//现在我们必须通知适配器有关新list filtered
filtered =(ArrayList< Item_Category>)results.values;
notifyDataSetChanged();


$ b


解决方案

创建适配器到自定义网格视图或列表视图,在过滤中都有一些问题。你看,适配器不会过滤你的网格或列表视图中的数据。



我不能粘贴代码,但是我建议你看看执行 ArrayAdapter.class 。确保 ItemCategoryArrayAdapter.class 具有大多数存在于 ArrayAdapter.class 中的方法。



只要确保 ArrayAdapter.class 中的大部分方法都在 ItemCategoryArrayAdapter.class



这会修复它...


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();
        }

    }
解决方案

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...

这篇关于自定义过滤器不能在android中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 02:27