2009-12-13 31 views
6

Có ai có thể làm sáng tỏ cách nhận danh sách liên hệ từ Android không ?.Danh sách liên hệ Android

Tôi chỉ muốn có danh sách tương tự như trong ứng dụng trình quay số. Nhưng tôi nhận được nhiều địa chỉ liên hệ không có trong danh sách trình quay số bằng mã bên dưới.

ContentResolver cr = getContentResolver(); 
Cursor cursor = cr.query(Contacts.People.CONTENT_URI, null, null, null, Contacts.ContactMethods.DEFAULT_SORT_ORDER); 
startManagingCursor(cursor); 

Xin cảm ơn trước.

Trả lời

2

Điều bạn có vẻ ổn. Bạn có thể xây dựng trên "nhận được rất nhiều địa chỉ liên lạc không có trong danh sách quay số" không? Có phải Android đang tạo nên mọi người không? Hoặc là bạn đang nhìn thấy những người có địa chỉ email nhưng không có số điện thoại (do đó có thể không hiển thị trong Trình quay số)?

Lưu ý rằng Contacts.People dành cho Android 1.6 trở xuống. Nhà cung cấp đó không được chấp nhận bắt đầu với Android 2.0, được thay thế bởi nhóm nhà cung cấp ContactsContract.

+0

tôi tưởng tượng rất nhiều các nhà phát triển sẽ không sử dụng ContactsContract vì họ muốn 1.6 tương thích ... – Eno

0

Vâng, cảm ơn câu trả lời trước tiên. Chỉ để làm sáng tỏ điều này.

Tôi chỉ muốn nhận email cho các địa chỉ liên hệ trên điện thoại của mình. Nhóm "MyContacts". Tôi thấy đây là nhóm ContactList Activity sử dụng.

tôi xong làm somethig như thế này:

c = cr.query(myGroupUri, mEmailsProjection, null, null, null); 
.... 

c.close(); 

c = cr.query(
    Contacts.ContactMethods.CONTENT_URI, 
     mContactsProjection, contactIds, null, null 
); 
.... 
c.close(); 

Chỉ cần truy vấn nhóm là người đầu tiên và sau đó là bàn email.

6

Hãy thử đoạn này:

import android.app.ListActivity; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.provider.ContactsContract; 
import android.provider.ContactsContract.CommonDataKinds.Phone; 
import android.widget.SimpleCursorAdapter; 

public class ContactList extends ListActivity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 


     Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[] {Phone._ID, Phone.DISPLAY_NAME, Phone.NUMBER}, null, null, null); 

     startManagingCursor(cursor); 

     String[] from = new String[] { Phone.DISPLAY_NAME, Phone.NUMBER}; 

     int[] to = new int[] { R.id.name_entry, R.id.number_entry}; 

     SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list_entry, cursor, from, to); 
     this.setListAdapter(adapter); 
    } 
} 

tập tin XML là:

list_entry.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:padding="6dip"> 
     <TextView 
      android:id="@+id/name_entry" 
      android:layout_width="fill_parent" 
      android:layout_height="0dip" 
      android:layout_weight="1" 
      android:gravity="center_vertical" 
     android:textSize="18dip"/> 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="0dip" 
      android:layout_weight="1" 
      android:id="@+id/number_entry" 
      android:singleLine="true" 
      android:ellipsize="marquee" 
     android:textSize="18dip"/> 
    </LinearLayout> 
+2

Yêu cầu nếu không thì sẽ hữu ích. –

+1

startManagingCursor không được dùng nữa ... – drulabs

+0

@KKD: Điều gì sẽ thay thế? –

1

This là thực hiện cơ bản của Android Hoạt động danh sách liên lạc.

0

hãy thử sử dụng mục đích để đi đến danh sách liên lạc

  startActivityForResult(new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI),1);} 
Các vấn đề liên quan