I'm making an Android app, and need to call the phone's contact list. I need to call the contacts list function, pick a contact, then return to my app with the contact's name. Here's the code I got on the internet, but it doesnt work.
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
public class Contacts extends ListActivity {
private ListAdapter mAdapter;
public TextView pbContact;
public static String PBCONTACT;
public static final int ACTIVITY_EDIT=1;
private static final int ACTIVITY_CREATE=0;
// Called when the activity is first created.
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Cursor C = getContentResolver().query(People.CONTENT_URI, null, null, null, null);
startManagingCursor(C);
String[] columns = new String[] {People.NAME};
int[] names = new int[] {R.id.row_entry};
mAdapter = new SimpleCursorAdapter(this, R.layout.mycontacts, C, columns, names);
setListAdapter(mAdapter);
} // end onCreate()
// Called when contact is pressed
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Cursor C = (Cursor) mAdapter.getItem(position);
PBCONTACT = C.getString(C.getColumnIndex(People.NAME));
// RHS 05/06
//pbContact = (TextView) findViewById(R.id.myContact);
//pbContact.setText(new StringBuilder().append("b"));
Intent i = new Intent(this, NoteEdit.class);
startActivityForResult(i, ACTIVITY_CREATE);
}
}
Be careful while working with android contact list.
Reading contact list in above methods work on most android devices except HTC One and Sony Xperia. It wasted my six weeks trying to figure out what is wrong!
Most tutorials available online are almost similar - first read "ALL" contacts, then show in
Listview
withArrayAdapter
. This is not memory efficient solution. Instead of looking for solutions on other websites first, have a look at developer.android.com. If any solution is not available on developer.android.com you should look somewhere else.The solution is to use
CursorAdapter
instead ofArrayAdapter
for retrieving contact list. UsingArrayAdapter
would work on most devices, but it's not efficient. TheCursorAdapter
retrieves only a portion of contact list at run time while theListView
is being scrolled.Retrieving a List of Contacts: Retrieving a List of Contacts
Looking around for an API Level 5 solution using ContactsContract API you could slightly modify the code above with the following:
And then in onActivityResult use the column name:
Here is the code snippet for get contact:
}
I use the code provided by @Colin MacKenzie - III. Thanks a lot!
For someone who are looking for a replacement of 'deprecated' managedQuery:
1st, assuming using v4 support lib:
2nd:
3rd,
4th, override callbacks:
5th:
6th, the code above, except that I change the signature param from Intent to Cursor:
7th, call initLoader:
8th, don't forget this piece of code
References:
Android Fundamentals: Properly Loading Data
Initializing a Loader in an Activity
To my surprise you do not need users-permission CONTACT_READ to read the names and some basic information (Is the contact starred, what was the last calling time). However you do need permission to read the details of the contact like phone number.