ambit
ambit

Reputation: 1119

Using Textwatcher with a simpleexpandablelistview in Android

In my previous app, I had a listview and and edittext at the bottom of it. I have been using the textwatcher in the edittext to filter a listview. The contents of the listview come from a simplecursoradapter.

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

In my current app, I have an expandablelistview. I would like to use a similar feature like filtering the content of the expandablelistview.

Am not sure if I can make use of textwatcher for this. Is there any other way to get this done or can I use textwatcher in this as well. ?

This is the relevant portion of code:

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

Upvotes: 0

Views: 262

Answers (1)

ambit
ambit

Reputation: 1119

The way I got this to work was that in the onTextChanged() method, I called up a function which would query the database and get the filtered data. I repopulate the expandable listview again based on the query results.

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

Upvotes: 1

Related Questions