RufusInZen
RufusInZen

Reputation: 2189

Filtering a cursor with WHERE condition

I'm trying to filter the contacts list by modifying the selection of my cursor when an OnTextChange happens in my EditText. The problem: the list stays as it is, i.e. it does not get updated as per the filter. What am I doing wrong? I suspect it's my sql query (specifically GLOB part)?

digitsText.addTextChangedListener(new TextWatcher(){
  public void onTextChanged(CharSequence s, int start, int before, int count){
    filterText = digitsText.getText().toString();
    WHERE_CONDITION = ContactsContract.Data.DATA1 + " GLOB '*" + filterText + "*'";
    cursor = getContentResolver().query(URI, PROJECTION, WHERE_CONDITION, null, SORT_ORDER);
    startManagingCursor(cursor);
    setListAdapter(adapter);
  }
});

Upvotes: 0

Views: 433

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007296

While you generate a new Cursor, you do not actually do anything with it. You need to either:

  • call swapCursor() on your CursorAdapter, if you are on API Level 11 or higher
  • call changeCursor() on your CursorAdapter (which may be the better option anyway, if you will not be needing the old Cursor, as changeCursor() will close it for you)
  • create a new CursorAdapter and call setListAdapter() using the new adapter

Upvotes: 1

Related Questions