-1

删除我曾尝试这个代码,但是当我点击删除菜单上没有数据从列表视图失去ContextItemSelected如何删除菜单联系人数据ContextItemSelected列表视图的Android

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

有另一种方式来删除联系人?

+0

“尝试”代码不会帮助任何人。了解你的概念。 – JoxTraex

回答

0

上下文项目选定值,你还必须检查在列表视图项上下文菜单中创建,要做到这一点,你可以写

if(itemID == 1) 

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

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

在index_id的你有在其上创建上下文菜单的列表视图项,之后你可以触发查询方法以及你想要用数据做什么。 :)

+0

我改变了它,但ctx变得不确定。如何定义它? – Fuji

相关问题