I'm showing contacts in a ListView and trying to filter the same list using an EditText. But when I type something, filtering is not happening, though the typed text is coming in Logcat. Here is my onCreate:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_invite);
EditText filterText = findViewById(R.id.search_box);
filterText.addTextChangedListener(filterTextWatcher);
// The contacts from the contacts content provider is stored in this cursor
mMatrixCursor = new MatrixCursor(new String[]{"_id", "name", "details"});
// Adapter to set data in the listview
mAdapter = new SimpleCursorAdapter(getBaseContext(),
R.layout.contact_layout, null, new String[]{"name", "details"}, new int[]{R.id.tv_name, R.id.tv_details}, 0);
// Getting reference to listview
ListView lstContacts = findViewById(R.id.lst_contacts);
// Setting the adapter to listview
lstContacts.setAdapter(mAdapter);
// Creating an AsyncTask object to retrieve and load listview with contacts
ListViewContactsLoader listViewContactsLoader = new ListViewContactsLoader();
// Starting the AsyncTask process to retrieve and load listview with contacts
listViewContactsLoader.execute();
}
This is my TextWatcher:
private TextWatcher filterTextWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
Log.d(TAG, "onTextChanged: " + s);
mAdapter.getFilter().filter(s.toString());
}
};
What am I doing wrong here? Can someone help?
EDIT: Adding my ListViewContactsLoader.
private class ListViewContactsLoader extends AsyncTask<Void, Void, Cursor> {
@Override
protected Cursor doInBackground(Void... params) {
Uri contactsUri = ContactsContract.Contacts.CONTENT_URI;
// Querying the table ContactsContract.Contacts to retrieve all the contacts
Cursor contactsCursor = getContentResolver().query(contactsUri,
null, null, null,
ContactsContract.Contacts.DISPLAY_NAME + " ASC ");
if (contactsCursor.moveToFirst()) {
do {
long contactId = contactsCursor.getLong(contactsCursor
.getColumnIndex("_ID"));
Uri dataUri = ContactsContract.Data.CONTENT_URI;
// Querying the table ContactsContract.Data to retrieve individual items like
// home phone, mobile phone etc corresponding to each contact
Cursor dataCursor = getContentResolver().query(dataUri,
null,
ContactsContract.Data.CONTACT_ID + "=" + contactId,
null, null);
String displayName = "";
String mobilePhone = "";
if (dataCursor.moveToFirst()) {
// Getting Display Name
displayName = dataCursor
.getString(dataCursor
.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
do {
// Getting Phone numbers
if (dataCursor
.getString(
dataCursor
.getColumnIndex("mimetype"))
.equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
switch (dataCursor.getInt(dataCursor
.getColumnIndex("data2"))) {
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
mobilePhone = dataCursor
.getString(dataCursor
.getColumnIndex("data1"));
break;
}
}
} while (dataCursor.moveToNext());
String details = "";
// Concatenating various information to single string
if (mobilePhone != null && !mobilePhone.equals(""))
details = mobilePhone + "\n";
// Adding id, display name, path to photo and other details to cursor
mMatrixCursor.addRow(new Object[]{
Long.toString(contactId), displayName, details});
}
} while (contactsCursor.moveToNext());
}
return mMatrixCursor;
}
@Override
protected void onPostExecute(Cursor result) {
// Setting the cursor containing contacts to listview
mAdapter.swapCursor(result);
Log.d(TAG, "onPostExecute: " + result);
}
}
You need to reset the ListViewadapter also after adding the text in EditText.
Note: resetData() is custom method in which you have to replace the List data with new List Data that appears after applying the Filter. Then finally notify the Adapter.