Android Contact List
Can anyone shed a light on how to get contact list from android?.
I just want to get the same list as in the dialer app. But im getting a lots of contacts that are not on the dialer list with the c开发者_JAVA技巧ode below.
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(Contacts.People.CONTENT_URI, null, null, null, Contacts.ContactMethods.DEFAULT_SORT_ORDER);
startManagingCursor(cursor);
Thanks in advance.
Try this snippet:
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);
}
}
XML file is:
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>
What you have seems fine. Could you elaborate on "getting a lots of contacts that are not on the dialer list"? Is it that Android is making up people? Or is it that you are seeing people with email addresses but no phone numbers (who therefore might not show up in the Dialer)?
Note that Contacts.People
is for Android 1.6 and below. That provider is deprecated starting with Android 2.0, replaced by the ContactsContract
set of providers.
This is basic implementation of android contact list Activity.
Well, thanks for the answer first. Just to shed a light on this.
I just wanted to get emails only for the contacts on my phone. The "MyContacts" group. I saw this is the group ContactList Activity uses.
I finished doing somethig like this:
c = cr.query(myGroupUri, mEmailsProjection, null, null, null);
....
c.close();
c = cr.query(
Contacts.ContactMethods.CONTENT_URI,
mContactsProjection, contactIds, null, null
);
....
c.close();
Just queried the group first and then the emails table.
try using intent to go to the contact list
startActivityForResult( new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI),1);}
精彩评论