I am trying to add a Search Contact feature in my app.
Everything is working fine but the search contact function is not working.
I trying to implement this feature for 2 days but i don't where i am doing mistake. If anybody can figured out the mistake please write the answer.
private void getContactResults () {
//TODO get contacts code here
Toast.makeText(this, "Getting contacts ....", Toast.LENGTH_LONG).show();
li = (ListView) findViewById(R.id.list);
Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
startManagingCursor(cursor);
final String phoneNumber = ContactsContract.CommonDataKinds.Phone.NUMBER;
final String phoneName = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME;
final String phoneID = ContactsContract.CommonDataKinds.Phone._ID;
final String[] ContactsList = {phoneName, phoneNumber, phoneID};
final int[] to = {android.R.id.text1, android.R.id.text2};
final SimpleCursorAdapter ArrayAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursor, ContactsList, to);
li.setAdapter(ArrayAdapter);
li.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
new ArrayList<>(Arrays.asList((ContactsList)));
// Filter contacts by search
li.setTextFilterEnabled(true);
ArrayAdapter.setFilterQueryProvider(new FilterQueryProvider() {
@Override
public Cursor runQuery(CharSequence constraint) {
Cursor cur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
return cur;
}
});
EditText queryText = (EditText) findViewById(R.id.search_box);
queryText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {
}
@Override
public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {
// This is the filter in action
ArrayAdapter.getFilter().filter(s.toString());
// Don't forget to notify the adapter
ArrayAdapter.notifyDataSetChanged();
}
@Override
public void afterTextChanged(final Editable s) {
// This is the filter in action
ArrayAdapter.getFilter().filter(s.toString());
// Don't forget to notify the adapter
ArrayAdapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, "Data changed", Toast.LENGTH_SHORT).show();
}
});
The error is in the code above. It doesn't look like you are using the
constraintvalueIt should be like the
I would also advise not using null, as per the android documentation due to performance issue that can arise.