2013-04-19 29 views
5

Tôi đang cố hiển thị danh sách Liên hệ từ điện thoại người dùng trong chế độ xem Danh sách bên trong ứng dụng. Tôi có thể lấy danh bạ nhưng một số Danh bạ sẽ có các số điện thoại di động khác nhau nên tôi muốn cho người đó biết nhiều lần.Android Tìm nạp Danh bạ vào Ứng dụng

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

Muốn hiển thị người dùng nhiều lần bằng số con số của anh ấy. Liệu tôi có thể liệt kê danh sách ngắn gọn chỉ dựa trên số điện thoại di động dài 10 chữ số không?

Example 

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

Name: Sarah 
Number 1: xxxxxxxxx 

này sẽ trả về là ba Danh sách các đầu như sau

John Doe xxxxxxxxx 
John Doe xxxxxxxxx 
Sarah  xxxxxxxxx 
+0

Câu hỏi của bạn là gì? – mach

+0

@mach Muốn hiển thị người dùng nhiều lần bằng số con số anh ta có. Liệu tôi có thể liệt kê danh sách ngắn gọn chỉ dựa trên số điện thoại di động dài 10 chữ số không? –

+0

Bạn không muốn sử dụng ExpandableListView và có các số dưới dạng phần tử con không? – mach

Trả lời

2

Bạn có thể thử một cái gì đó như thế này

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

Đoạn code dưới đây sẽ lấy tất cả các địa chỉ liên lạc với số điện thoại. Có thể trùng lặp bởi vì cùng một liên hệ có thể thuộc về nhóm khác nhau. Bạn phải lặp mặc dù và loại bỏ các bản sao.

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); 
Các vấn đề liên quan