0

在我以前的应用程序中,我在它的底部有一个列表视图和编辑文本。我一直在 edittext 中使用 textwatcher 来过滤列表视图。列表视图的内容来自一个简单的光标适配器。

 edittext.addTextChangedListener(filterTextWatcher);

        cursor.setFilterQueryProvider(new FilterQueryProvider() {
            @Override
            public Cursor runQuery(CharSequence constraint) {
                Cursor cursor=mDbHelper.fetchFilteredNotes(edittext.getText().toString());
                return cur;
            }
        });

private TextWatcher filterTextWatcher = new TextWatcher() {

        public void afterTextChanged(android.text.Editable s) {
        };

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {};

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            simplecursoradapter.getFilter().filter(s.toString());
        };
    };

在我当前的应用程序中,我有一个可扩展列表视图。我想使用类似的功能,例如过滤可扩展列表视图的内容。

我不确定我是否可以为此使用 textwatcher。有没有其他方法可以完成这项工作,或者我也可以在其中使用 textwatcher。?

这是代码的相关部分:

private DbAdapter mDbHelper;    
List<Map<String, String>> groupData,groupDataCat;
List<List<Map<String, String>>> childData,childDataCat;
Map<String, String> curGroupMap;
List<Map<String, String>> meaning=null;
Map<String, String> curChildMap;
private static final String WORD = "WORD";
private static final String MEANING = "MEANING";
SimpleExpandableListAdapter mAdapter;
ExpandableListView elvCat;


temp=mDbHelper.fetchAllNotes();
temp.moveToFirst();
getActivity().startManagingCursor(temp);

if(temp.moveToFirst()){
   groupDataCat = new ArrayList<Map<String, String>>();
   childDataCat = new ArrayList<List<Map<String, String>>>();

       for (int i = 0; i < temp.getCount(); i++) {
        Map<String, String> catGroupMap = new HashMap<String, String>();
        groupDataCat.add(catGroupMap);
        catGroupMap.put(WORD, temp.getString(temp.getColumnIndexOrThrow(DbAdapter.KEY_WORD)));

        List<Map<String, String>> meaning = new ArrayList<Map<String, String>>();
         Map<String, String> catChildMap = new HashMap<String, String>();
         meaning.add(catChildMap);
         catChildMap.put(MEANING, temp.getString(temp.getColumnIndexOrThrow(DbAdapter.KEY_MEANING)));
         childDataCat.add(meaning);
         temp.moveToNext();
       }
    }

    mAdapter = new SimpleExpandableListAdapter(
            WordListFragment.this.getActivity(),
            groupDataCat,
            R.layout.word_list_item,
            new String[] {WORD},
            new int[] { R.id.tvWord },
            childDataCat,
            R.layout.meaning_list_item,
            new String[] {MEANING},
            new int[] { R.id.tvMeaning}
    );

  view=inflater.inflate(R.layout.word_list_temp, container, false);
    elvCat = (ExpandableListView) view.findViewById(R.id.elvWord);
    elvCat.setAdapter(mAdapter);


    return view;
}
4

1 回答 1

1

我让它工作的方式是在 onTextChanged() 方法中,我调用了一个函数,该函数将查询数据库并获取过滤后的数据。我根据查询结果再次重新填充可扩展列表视图。

private void populateList(String filter) {
            temp = mDbHelper.fetchSugNotes(filter);


        temp.moveToFirst();
        this.startManagingCursor(temp);
        groupDataCat = new ArrayList<Map<String, String>>();
        childDataCat = new ArrayList<List<Map<String, String>>>();

        for (int i = 0; i < temp.getCount(); i++) {
            Map<String, String> catGroupMap = new HashMap<String, String>();
            groupDataCat.add(catGroupMap);
            catGroupMap.put(WORD, temp.getString(temp
                    .getColumnIndexOrThrow(DbAdapter.KEY_WORD)));

            List<Map<String, String>> meaning = new ArrayList<Map<String, String>>();
            Map<String, String> catChildMap = new HashMap<String, String>();
            meaning.add(catChildMap);
            catChildMap.put(MEANING, temp.getString(temp
                    .getColumnIndexOrThrow(DbAdapter.KEY_MEANING)));
            childDataCat.add(meaning);
            temp.moveToNext();
        }

        mAdapter = new SimpleExpandableListAdapter(this, groupDataCat,
                R.layout.word_list_item, new String[] { WORD },
                new int[] { R.id.tvWord }, childDataCat,
                R.layout.meaning_list_item, new String[] { MEANING },
                new int[] { R.id.tvMeaning });

        elvCat.setAdapter(mAdapter);
于 2013-08-20T13:54:34.363 回答