First of all when you need to read Contacts on android, you need the user's permission that you will read their contact list. To do this
* Open AndroidManifest.xml and add the following codes in between manifest tags
<uses-permission android:name="android.permission.READ_CONTACTS"> </uses-permission>
- On eclipse, open AndroidManifest.xml then go to Permissions Tab, Click Add, Click on "Uses Permission", on the right side, on the name field select android.permission.READ_CONTACTS
After adding the permission, let us go to the codes: On your codes,
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;
public class Main extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Cursor contactsCursor = this.managedQuery(People.CONTENT_URI, null, null, null, null);
startManagingCursor(contactsCursor);
String[] columnsToMap = new String[] {People.NAME};
int[] mapTo = new int[] {android.R.id.text1};
ListAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, contactsCursor, columnsToMap, mapTo);
this.setListAdapter(mAdapter);
}
}A brief explanation to the core codes:
Cursor contactsCursor = this.managedQuery(People.CONTENT_URI, null, null, null, null);
-- This would query our Contacts, you can treat this somehow like SQL statement, we query the People.CONTENT_URI model, you can find out what the null represents in here
startManagingCursor(contactsCursor);
-- Tell it to start managing this cursor
String[] columnsToMap = new String[] {People.NAME};
-- As stated, you can treat this as sql, what we just did here is that we put the field People.NAME to an Array of String, you may have many fields as possible in this array. Here you can see we use People.NAME instead of a string field name, well this is the same except the class People has a static variable named NAME which translate to the string field name. The whole list of possible fields are in here
int[] mapTo = new int[] {android.R.id.text1};
-- This part is confusing but to explain it, we would map the People.NAME field to a textbox, now where did we specify the textbox?
ListAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, contactsCursor, columnsToMap, mapTo);
-- SimpleCursorAdaptor would bind/map our Cursor with our layout, you can refer here on what the parameters represent. The 2nd parameter here is layout, android by default has a lot of layout given to you by default, one of the layout that it has is the simple_list_item_1 layout, the 2nd comment on this thread would help you understand why we need the textbox mentioned on the top. The 3rd parameter is our cursor, the fourth is the fields/columns we need from our cursor to be mapped to our fifth parameters.
Hope i didn't confuse you and hope it helps :)
Update History
Jan 17, 2012 - Visual Update
6 comments:
Do you have the XML that you used for the listview, and the XML for each row item? I am looking for formatting ideas on how to make the listview presentable.
Also, I noticed that in testing my program, that if on of the columns I am displaying in the list contains a fairly long text, the remaining columns are not shown. How can I add a horizontal scroll?
Thanks.
- Bryan
bryan, you can try to refer to the documentation http://developer.android.com/reference/android/app/ListActivity.html There is a topic there on how to format it on a custom way.
bryan, sry seems the documentation is not updated. Ill create a post on how to customize the listview when I got home from my vacation. Tnx
Does anyone know why my new contacts do not show up alphabetically anymore on my droid? The only way to find them is to search them and I need to spell them right to find them.
You just saved my life. I have been following this tutorial
http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
on custom listviews but it's way to complicated for what I wanted to achieve. Yours is the most clear and concise tutorial on how to have custom database fields in a listview. In my case I wanted to convert "2011-11-30 05:44:12" to "30 Nov 05:44" and this tutorial did just the job. Thank you.
Hey,
the Contacts.People api is deprected for quite some time now. The new approch using the ContactsContract was introduced already at api level 5. The suggested way to get information on Contacts is now using the ContactsContract.RawContacts and ContactsContract.Contacts content provider. For more information on that topic you can take a look at http://blog.app-solut.com/2011/03/working-with-the-contactscontract-to-query-contacts-in-android/
Post a Comment