2013-04-19 5 views
5

사용자 목록의 연락처 목록을 애플리케이션의 목록보기에 표시하려고합니다. 연락처를 가져올 수 있지만 연락처 중 일부에는 여러 핸드폰 번호가 있으므로 그 사람을 여러 번 보여주고 싶습니다.Android가 애플리케이션에 연락처 가져 오기

Cursor c = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 
    String name, number = ""; 
    String id; 
    c.moveToFirst(); 
    for (int i = 0; i < c.getCount(); i++) { 
     name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
     id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID)); 

     if (Integer.parseInt(c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { 
      Cursor pCur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, 
        null); 
      while (pCur.moveToNext()) { 
       number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
      } 
     } 
     Log.i("name ", name + " "); 
     Log.i("number ", number + " "); 
     c.moveToNext(); 

사용자가 표시 할 횟수만큼 표시 할 수 있습니다. 나는 심지어 길이가 10 자리 인 휴대 전화 번호만으로 짧은 목록을 작성할 수 있습니까? 당신은 코드는 아래 전화 번호 모든 연락처를 가져옵니다이

List<PhoneItem> phoneNoList = new ArrayList<PhoneItem(); 
Cursor c = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 
String name, number = ""; 
String id; 
c.moveToFirst(); 
for (int i = 0; i < c.getCount(); i++) { 
    name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
    id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID)); 

    if (Integer.parseInt(c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { 
     Cursor pCur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, 
       null); 
     while (pCur.moveToNext()) { 
      phoneNoList.add(new PhoneItem(name, pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)))); 
     } 
    } 
    c.moveToNext(); 


} 

for (PhoneItem row : phoneNoList) { 
    Log.i("name", row.name); 
    Log.i("number", row.number+""); 
} 

[...] 

private class PhoneItem { 
    String name; 
    String phone; 

    public PhoneItem(String name, String phone) { 
     this.name = name; 
     this.phone = phone; 
    } 
} 
+0

무엇이 당신 질문입니까? – mach

+0

@mach 사용자의 수만큼 표시 할 수 있습니다. 나는 심지어 길이가 10 자리 인 휴대 전화 번호만으로 짧은 목록을 작성할 수 있습니까? –

+0

ExpandableListView를 사용하고 숫자를 하위 요소로 사용하고 싶지 않으십니까? – mach

답변

2

을 다음과 같이

Example 

Name: John Doe 
Number 1: xxxxxxxxx 
Number 2: xxxxxxxxx 

Name: Sarah 
Number 1: xxxxxxxxx 

이 세 가지 목록 항목 수 반환해야합니다. 동일한 연락처가 다른 그룹에 속할 수 있기 때문에 중복 될 수 있습니다. 루프를 반복하고 중복을 제거해야합니다.

String[] projection = new String[]{ ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, 
            ContactsContract.CommonDataKinds.Phone.NUMBER,}; 
String selection = ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER + "=?" ; 
String[] selectionArgs = new String[] {"1"};        
Cursor c = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
            projection, 
            selection, 
            selectionArgs, 
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME 
            + ", " + ContactsContract.CommonDataKinds.Phone.NUMBER); 
1

처럼 뭔가를 시도 할 수 있습니다

John Doe xxxxxxxxx 
John Doe xxxxxxxxx 
Sarah  xxxxxxxxx