2011-08-18 81 views
2

我想刪除具有特殊名稱的聯繫人。如何使用名稱刪除Android 2.2中的聯繫人?

我嘗試下面的代碼來找到我的目標:

public Cursor searchByName(String name) 
    { 
     try 
     { 
      ContentResolver cr = getContentResolver(); 
      Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null); 
      if (cur.getCount() > 0) 
      { 
       while (cur.moveToNext()) 
       { 
        //String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)); 
        String Name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
        if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) 
        { 
         if(Name.equals(name)) 
         { 
          return cur; 
         } 
        } 
       } 
      } 
     } 
     catch (Exception e) 
     { 
      Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show(); 
     } 
     return null; 
    } 

及以下功能將其刪除:

public void removeContactByName(String name) 
{ 
    try 
    { 
     Cursor cur = searchByName(name); 
     if(cur!=null) 
     { 
      Uri uri ; // what should it be? 
      getContentResolver().delete(uri,null, null); 
     } 
     else 
     { 
      Toast.makeText(getApplicationContext(), "Not Found",Toast.LENGTH_SHORT).show(); 
     } 
    } 
    catch (Exception e) 
    { 
     Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show(); 
    } 
} 

但我應該URI是什麼?

回答

1

你可以搜索Contact的名字,並把它_id,在此之後,通過_id

+0

感謝刪除....怎麼刪除由id.what是功能? –

+0

http://stackoverflow.com/questions/527216/how-to-remove-a-contact-programmatically-in-android –