-1

我已经尝试过这段代码,但是当我单击删除菜单 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;
    }

还有其他方法可以删除联系人吗?

4

1 回答 1

0

在上下文项选定值上,您还必须检查在哪个列表视图项上下文菜单中创建,为此您可以编写

if(itemID == 1)

{
AdapterView.AdapterContextMenuInfo menuinfo;
menuinfo = (AdapterContextMenuInfo)item.getMenuInfo();

int index_id = menuinfo.position;
deletedata(index_id);

在 index_id 中,您获得了创建上下文菜单的列表视图项,之后您可以触发查询方法以及您想要对数据执行的任何操作。:)

于 2012-05-16T04:36:18.087 回答