How to create a ListView filter with SimpleCursorAdapter on Android

I will make a filter for a list from am SimpleCursorAdapter. I see a toast following my example, but nothing happens to the filter. I found an example with setFilterQueryProvider. But I do not know to implement this. Here's my code:

    Cursor entryCursor = mDatabase.query(
            "entrys",
            new String[] {
                    "_id",
                    "name",
                    "address"
            },
            null, null, null, null, null);
    startManagingCursor(entryCursor);

    final SimpleCursorAdapter entryAdapter =
        new SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_2,
                entryCursor,
                new String [] {"name",
                               "address"},
                new int[] { android.R.id.text1,
                            android.R.id.text2}
                );

    setListAdapter(entryAdapter);

    final ListView mList = (ListView) findViewById(android.R.id.list);
    mList.setAdapter(entryAdapter);

    final EditText et_search = (EditText) findViewById(R.id.listview_search);
    et_search.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
            // Abstract Method of TextWatcher Interface.
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // Abstract Method of TextWatcher Interface.
        }
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // Abstract Method of TextWatcher Interface.
            entryAdapter.getFilter().filter(s.toString());
            Toast.makeText(getApplicationContext(), "Text changed", Toast.LENGTH_SHORT).show();
        }
    });

      

+3


source to share





All Articles