Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中使用上下文菜单删除联系人_Android - Fatal编程技术网

如何在android中使用上下文菜单删除联系人

如何在android中使用上下文菜单删除联系人,android,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().quer

我尝试使用下面的代码,使用按名称选择的上下文菜单项删除联系人

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);
    }

这取决于你的代码。如果onContextItemSelected在您的活动中:


ContactManager.deleteContact(MyActivity.this.getContext()、MyActivity.this.phone、MyActivity.this.name)

这取决于您的代码。如果onContextItemSelected在您的活动中:


ContactManager.deleteContact(MyActivity.this.getContext()、MyActivity.this.phone、MyActivity.this.name)

你有什么问题?将其放在这种情况下,例如MyActivity.deleteContact(…)ContactManager.deleteContact(ctx、电话、姓名);但ctx,电话,名字是红色下划线。我应该补充什么?你有什么问题?将其放在这种情况下,例如MyActivity.deleteContact(…)ContactManager.deleteContact(ctx、电话、姓名);但ctx,电话,名字是红色下划线。我应该补充什么?