id | title | subtitle | link | sample | sdk | dateupdated | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
D5AC9861-F18A-4AB1-4A10-87E98D40205E |
Read Contacts |
How to read contacts in Android and display the results in a list |
|
|
|
2017-10-17 |
Follow these steps to query contacts.
- Double-click the project in Visual Studio and navigate to project Properties.
- Select the Android Application panel in the Project Options dialog in the Properties navigator in VS.
- Click the Add Android Manifest button.
- Under Required permissions set the
READ_CONTACTS
permission and press OK. - Add a file named ContactItemView.axml under the Resources/layout folder containing the following XML.
<?xml version="1.0" encoding="UTF-8" ?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="15dip"/>
In a ListActivity
subclass, add the following code to the OnCreate
method:
var uri = ContactsContract.Contacts.ContentUri;
string[] projection = { ContactsContract.Contacts.InterfaceConsts.Id,
ContactsContract.Contacts.InterfaceConsts.DisplayName };
var cursor = ContentResolver.Query(uri, projection, null, null, null);
var contactList = new List<string> ();
if (cursor.MoveToFirst ()) {
do {
contactList.Add (cursor.GetString (
cursor.GetColumnIndex (projection [1])));
} while (cursor.MoveToNext());
}
ListAdapter = new ArrayAdapter<string> (this,
Resource.Layout.ContactItemView, contactList);
ContactsContract.Contacts
is the data contract that is used to interface with
contact data. The code queries contacts at the Uri defined by
ContactsContract.Contacts.ContentUri
, returning the columns selected from
ContactsContract.Contacts.InterfaceConsts
. It adds the results to
List<string>
instance that is used to bind to the user interface through
an ArrayAdapter
.