2012-05-09 31 views
1

我嘗試使用由名稱選擇的上下文菜單項刪除聯繫人的以下代碼。如何在android中使用上下文菜單刪除聯繫人

public static boolean deleteContact(Context ctx, String phone, String name) { 

    Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone)); 
    Cursor cur = ctx.getContentResolver().query(contactUri, null, null, 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, null, null); 
        return true; 
       } 

      } while (cur.moveToNext()); 
     } 

    } catch (Exception e) { 
     System.out.println(e.getStackTrace()); 
    } 
    return false; 
} 

但我不知道如何把這個代碼到我的上下文菜單項選擇

public boolean onContextItemSelected(MenuItem item) 
{ 
    switch (item.getItemId()) 
    { 
    case 0: 

     return true; 
    } 
    return super.onContextItemSelected(item); 
    } 
+0

你的問題是什麼?把它放在這種情況下 – breceivemail

+0

例如MyActivity.deleteContact(...) – breceivemail

+0

ContactManager.deleteContact(ctx,phone,name); – Fuji

回答

1

它取決於你的代碼。如果onContextItemSelected在您的活動中:

ContactManager.deleteContact(MyActivity.this.getContext(),MyActivity.this.phone,MyActivity.this.name);

+0

它運作良好,但手機號碼爲空如何刪除這一個..... – NagarjunaReddy

相關問題