0

나는 listview이고 나는 registerForContextMenu이라고 불렀다. ContextMenu에는 하나의 메뉴 만 "연락처 삭제"가 있습니다. 다음은 삭제 메뉴를 클릭했을 때의 코드입니다 :Android에서 연락처를 삭제하는 방법은 무엇입니까?

public boolean onContextItemSelected(MenuItem item) 
{ 
    switch (item.getItemId()) 
    { 
    case DELETE_ID: 
     AdapterView.AdapterContextMenuInfo menuinfo; 
     menuinfo = (AdapterContextMenuInfo)item.getMenuInfo(); 
     int id = menuinfo.position; 
     delete(id); 
     return true; 
    } 
    return super.onContextItemSelected(item); 
    } 


private void delete(int id) 
{ 
    ContentResolver cr = getContentResolver(); 
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, 
     null, ContactsContract.Contacts._ID + "=" + id, null, null); 
    while (cur.moveToNext()) { 
     try{ 
      String lookupKey = cur.getString(cur.getColumnIndex(
       ContactsContract.Contacts.LOOKUP_KEY)); 
      Uri uri = Uri.withAppendedPath(ContactsContract. 
       Contacts.CONTENT_LOOKUP_URI, lookupKey); 
      System.out.println("The uri is " + uri.toString()); 
      cr.delete(uri, ContactsContract.Contacts._ID + "=" + id, null); 
     } 
    catch(Exception e) 
    { 
     System.out.println(e.getStackTrace()); 
    } 
     } 
} 

그러나 위의 코드를 사용하면 세부 정보가 지워지지 않습니다. 제 질문은 위의 코드 중 하나라도 누락되었거나 잘못 되었습니까?

+0

감사합니다 Mr.Peter G. – Fuji

답변

0

당신은 AdapterContextMenuInfo에서 id을 사용할 수 있습니다 : 지금 당신은 어댑터에서 선택한 행의 위치를 ​​나타내는 position, 그리고 상대의 id을 사용하고

long id = menuinfo.id; 

그 특정 행에 대해서는 id과 접촉 할 때 찾을 수 없을 것입니다.

+0

ID로 위치를 변경하고 id = int를 입력합니다. long으로 바꿉니다. 마침내 효과가있었습니다. 정말 고맙습니다. – Fuji

+0

@Fuji 죄송합니다. 'id'는 (는) 'long'값으로 설정해야합니다. – Luksprog

관련 문제