使用CustomView ListView上的“编辑文本”搜索功能

时间:2014-03-26 15:36:49

标签: android listview search android-edittext

我可以过滤掉自定义列表。

问题是我的自定义列表视图有4个文本字段。 当我搜索时得到结果,但如果我在列表行的不同字段中有一些重复的文本,那么过滤器返回相同数量的重复行。

如果说,我的条目是这两行数据{apple,apple,orange,apple},{grape,melon,mango,peaches} 我开始搜索苹果...我会在列表视图中看到3行重复数据,而不是一行

如何停止此重复?

这是我的代码:

adapter = new MyAdapter(
            this,
            list,
            R.layout.list_row,
            new String[] {fruit1, fruit2, fruit3, fruit4 },
            new int[] {R.id.fruit1, R.id.fruit2, R.id.fruit3,R.id.fruit4});

    populateList();
    setListAdapter(adapter);

    search.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text
            ListScreen.this.adapter.getFilter().filter(cs);   
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}

        @Override
        public void afterTextChanged(Editable arg0) {}
    });

  class MyAdapter extends SimpleAdapter{

    public PassAdapter(Context context, List<? extends Map<String, ?>> data, int resource,  String[] from, int[] to) {
        super(context, data, resource, from, to);
        }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view =  super.getView(position, convertView, parent);
        arrow = (ImageView) view.findViewById(R.id.arrow);
        data= (LinearLayout) view.findViewById(R.id.data);

        arrow.setImageResource(R.drawable.arrow_down);
        data.setVisibility(View.GONE);

        return view;
    }

这也是我的自定义过滤器代码 但这不会刷新我的清单

    @Override
    public Filter getFilter() {

        Filter filter = new Filter() {

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults res = new FilterResults();
                // We implement here the filter logic
                if (constraint == null || constraint.length() == 0) {
                    // No filter implemented we return all the list
                    res.values = tempList;
                    res.count = tempList.size();
                } else {
                    synchronized(this){
                        // We perform filtering operation
                        List<HashMap<String, String>> dataList = new ArrayList<HashMap<String, String>>();
                        for (HashMap<String, String> data : tempList) {
                            if (data.get("fruit1").toUpperCase().startsWith(constraint.toString().toUpperCase()))
                                dataList.add(data);
                        }

                        res.values = dataList;
                        res.count = dataList.size();

                    }
                }
                return res;
            }

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

            }
        };
        return filter;
    }

3 个答案:

答案 0 :(得分:1)

找到了解决方法

  search.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text
//                ListScreen.this.adapter.getFilter().filter(cs); 
            if(cs.length() == 0 || cs == null){
                list.clear();
                list.addAll(origList);
            }
            else{
                ArrayList<HashMap<String, String>> temp = new ArrayList<HashMap<String,String>>();
                for(HashMap<String, String> data : list){
                    if(data.get("fruit1").toLowerCase().contains(cs.toString().toLowerCase())){
                        temp.add(data);
                    }
                }
                list.clear();
                list.addAll(temp);
            }
            adapter.notifyDataSetChanged();
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}

        @Override
        public void afterTextChanged(Editable arg0) {}
    });

答案 1 :(得分:0)

在主要活动中;

  etsearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                    String text = etsearch.getText().toString().toLowerCase(Locale.getDefault());
                if(text.length()>2)
                subCropAdapter.filter(text);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

并且在适配器中;

public void filter(String charText){

    charText = charText.toLowerCase(Locale.getDefault());

    subCropModelList.clear();
    if (charText.length() == 0) {

        subCropModelList.addAll(subCropModelArrayList);

    } else {
        for (SubCropModel wp : subCropModelArrayList) {
            if (wp.getSubCropName().toLowerCase(Locale.getDefault()).contains(charText)) {
                subCropModelList.add(wp);
                //subCropModelArrayList.add(wp);
            }

        }

        subCropModelArrayList.clear();
        subCropModelArrayList.addAll(subCropModelList);
        notifyDataSetChanged();
    }

答案 2 :(得分:-1)

我认为你的解决方案很简单

只需添加adapter.notifyDataSetChanged();在onTextChanged方法中。

示例:

fc-cache -v -f

这将简单地解决问题。