Filtering with CursorAdapter

suggest change
// Get the reference to your ListView
ListView listResults = (ListView) findViewById(;

// Set its adapter

// Enable filtering in ListView

// Prepare your adapter for filtering    
adapter.setFilterQueryProvider(new FilterQueryProvider() {
    public Cursor runQuery(CharSequence constraint) {

        // in real life, do something more secure than concatenation
        // but it will depend on your schema
        // This is the query that will run on filtering
        String query = "SELECT _ID as _id, name FROM MYTABLE "
                       + "where name like '%" + constraint + "%' "
                       + "ORDER BY NAME ASC";
        return db.rawQuery(query, null);

Let’s say your query will run every time the user types in an EditText:

EditText queryText = (EditText) findViewById(;
    queryText.addTextChangedListener(new TextWatcher() {
        public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {


        public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {
            // This is the filter in action
            // Don't forget to notify the adapter

        public void afterTextChanged(final Editable s) {


Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents