I have tried this code but no data is lost from the listview when I clicked on the delete menu ContextItemSelected
public boolean onContextItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case DELETE_ID:
AdapterView.AdapterContextMenuInfo menuinfo;
menuinfo = (AdapterContextMenuInfo)item.getMenuInfo();
int id = menuinfo.position;
deleteContact(id);
populateContactList();
return true;
}
return super.onContextItemSelected(item);
}
public static boolean deleteContact(int id) {
Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone));
Cursor cur = ctx.getContentResolver().query(contactUri, null, ContactsContract.Contacts._ID + "=" + id, null, null);
try {
if (cur.moveToFirst()) {
do {
if (cur.getString(cur.getColumnIndex(PhoneLookup.DISPLAY_NAME)).equalsIgnoreCase(name)) {
String lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
ctx.getContentResolver().delete(uri,ContactsContract.Contacts._ID + "=" + id, null);
return true;
}
} while (cur.moveToNext());
}
} catch (Exception e) {
System.out.println(e.getStackTrace());
}
return false;
}
is there another way to delete a contact ?
on context item selected value, you also have to check in which listview item context menu is created , to do this you can write
in index_id you got the listview item on which context menu is created, after that you can fire query method and whatever you want to do with data. 🙂