Reputation: 2997
In this activity, i am fetching all contacts and showing in listView
and there is a EditText
to search the contact like, if i write "ni" then the listView
will show all contact starting with "ni". to do so i am using adapter.clear()
and adapter.addAll(matchedContacts
, all contact start with "ni") for every key stroke. But here the problem is that every call of adapter.clear()
is clearing the main list of contact that is contactList
(where i have stored all contacts) and filling it with matchedContacts
, beacause of that if i change EditText
the listView
is not populating with new contact.
Here is the code:
public class AddPerson2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addperson2);
EditText searchContact = (EditText)findViewById(R.id.contact_search);
final ListView listView = (ListView)findViewById(R.id.contact_list);
final ArrayList<String[]> contactList = new ArrayList<>();
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
, ContactsContract.CommonDataKinds.Phone.NUMBER}
, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
if (cursor != null){
if (cursor.getCount() > 0){
while (cursor.moveToNext()){
String[] contact = new String[2];
contact[0] = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
contact[1] = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactList.add(contact);
}
}
}
final ContactListAdapter contactListAdapter = new ContactListAdapter(this, contactList);
listView.setAdapter(contactListAdapter);
searchContact.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
String startingCharacter = s.toString().toLowerCase();
ArrayList<String[]> matchedContacts = new ArrayList<>();
for (String[] contact : contactList) {
if (contact[0].toLowerCase().startsWith(startingCharacter)){
matchedContacts.add(contact);
}
}
contactListAdapter.clear();
contactListAdapter.addAll(matchedContacts);
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
view.setBackgroundColor(getResources().getColor(R.color.personSelectBackground));
Log.wtf("showid", id+"");
}
});
}
}
Upvotes: 0
Views: 75
Reputation: 1007474
If ContactListAdapter
is an ArrayAdapter
, then clear()
is behaving as expected:
clear()
on the underlying ArrayList
, thereby emptying itnotifyDataSetChanged()
on the attached AdapterView
, to let it know about the changes in the dataPerhaps you want to be using filtering on the adapter, by calling getFilter().filter()
on the adapter. This sample app demonstrates this, though I use a SearchView
instead of an EditText
.
Upvotes: 2