当用户删除changehers时,如何使用searchView widjet搜索原始列表

时间:2016-07-20 06:27:11

标签: android searchview

我在ListView中实现了搜索功能。当用户输入文本时,它工作正常,但它改变了填充列表视图的列表,如果用户键入" hello"然后想要删除它并搜索其他内容,该列表只保存"你好"他找不到任何其他东西。

我的代码:

 class MyAdapter extends BaseAdapter implements CompoundButton.OnCheckedChangeListener,Filterable {
        private SparseBooleanArray mCheckStates;
        LayoutInflater mInflater;
        TextView  tv;
        CheckBox cb;
        ValueFilter filter;

        MyAdapter() {
            mCheckStates = new SparseBooleanArray(nameList.size());
            mInflater = (LayoutInflater) AddRoomatesScreen.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            return nameList.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            View vi = convertView;
            if (convertView == null)
                vi = mInflater.inflate(R.layout.contact_list_item, null);
            TextView tv = (TextView) vi.findViewById(R.id.textView3);

            cb = (CheckBox) vi.findViewById(R.id.checkBox);
            tv.setText(nameList.get(position));

            cb.setTag(position);
            cb.setChecked(mCheckStates.get(position, false));
            cb.setOnCheckedChangeListener(this);

            return vi;
        }

        public boolean isChecked(int position) {
            return mCheckStates.get(position, false);
        }

        public void setChecked(int position, boolean isChecked) {
            mCheckStates.put(position, isChecked);
            notifyDataSetChanged();
        }

        public void toggle(int position) {
            setChecked(position, !isChecked(position));
        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                                     boolean isChecked) {

            mCheckStates.put((Integer) buttonView.getTag(), isChecked);
        }

        @Override
        public Filter getFilter() {
            if (filter == null){
                filter = new ValueFilter();
            }
                return filter;

        }

          private class ValueFilter extends Filter {

                @Override
                protected FilterResults performFiltering(CharSequence constraint) {
                    FilterResults results = new FilterResults();

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

                                String name = nameList.get(i).toString();

                                filterList.add(name);
                            }
                        }
                        results.count = filterList.size();
                        results.values = filterList;
                    } else {
                        results.count = nameList.size();
                        results.values = nameList;
                    }
                    return results;

                }

                @Override
                protected void publishResults(CharSequence constraint, FilterResults results) {
                    nameList = (ArrayList<String>) results.values;
                    notifyDataSetChanged();
                }
            }
}

活动主要:

List<String> nameList = new ArrayList<String>();
    List<String> phoneList = new ArrayList<String>();
    MyAdapter adapter;
    SearchView searchView;

super.onCreate(savedInstanceState);
        setContentView(R.layout.add_roomate);
 ListView lv = (ListView) findViewById(R.id.lv);
        adapter = new MyAdapter();
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(this);
        lv.setItemsCanFocus(false);
        lv.setTextFilterEnabled(true);
        searchView = (SearchView) findViewById(R.id.search_view);
        searchView.setOnQueryTextListener(this);

@Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        adapter.getFilter().filter(newText);
        return false;
    }
}

0 个答案:

没有答案