【问题标题】:How to delete a Contact in Android 2.2 with name ?如何在 Android 2.2 中删除带有姓名的联系人?
【发布时间】:2011-10-29 13:39:23
【问题描述】:

我想删除一个特殊名字的联系人。

我尝试下面的代码来找到我的目标:

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 应该是什么?

【问题讨论】:

    标签: android contacts


    【解决方案1】:

    你可以通过名字搜索Contact,得到_id,然后用_id删除

    猜你喜欢
    • 2011-05-19
    • 2012-10-11
    • 1970-01-01
    • 2011-09-13
    • 1970-01-01
    • 2015-11-14
    • 2021-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多