I. Define the layout file yourself List_item_users.xml
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
tools:context= "${packagename}.${activityclass}" >
<imageview
Android:id= "@+id/img"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_below= "@+id/first"
android:src= "@drawable/ic_launcher"/>
<textview
Android:id= "@+id/name"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_alignbottom= "@+id/img"
android:layout_torightof= "@+id/img"
android:text= "Name"/>
<textview
Android:id= "@+id/first"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_alignparentleft= "true"
Android:layout_alignparenttop= "true"
android:text= "First"/>
</RelativeLayout>
Second, the main layout file Activity_main.xml layout a ListView
Third, database reading and content Association
public class Mainactivity extends Activity {
Private ListView lv_users;
Private Simplecursoradapter adapter;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Lv_users = (ListView) Findviewbyid (r.id.lv_users);
InitData ();
}
private void InitData () {
Contentresolver resolver =getcontentresolver ();
Cursor c is an indicator of the definition. Gets the contents of the database in query (table address, new string[]{to get the name of the column},. )
Cursor C = resolver.query (ContactsContract.RawContacts.CONTENT_URI,
New string[] {"_id", ContactsContract.Contacts.DISPLAY_NAME, "Phonebook_label"},
NULL, NULL, NULL);
The adapter adapter is a layout file that points to its own definition. (in context, define the layout file name yourself.) New string[]{What to lay out},new int[]{content})
adapter = new Simplecursoradapter (this, r.layout.list_item_users, C,
New string[] {ContactsContract.Contacts.DISPLAY_NAME, "Phonebook_label"}, new int[] {
R.id.name,r.id.first},
Cursoradapter.flag_register_content_observer);
Add an adapter (also understood as a layout file that you define yourself) to the main layout file
Lv_users.setadapter (adapter);
}
}
In Androidmanifest.xml you should pay attention to join permissions:
<instrumentation android:name= "Android.test.InstrumentationTestRunner" android:targetpackage= " Com.example.readcontactsdemo "></instrumentation>
<uses-permission android:name= "Android.permission.READ_CONTACTS"/>
<uses-permission android:name= "Android.permission.WRITE_CONTACTS"/>
android:minsdkversion= "11" changed from 8 to 11
Get a contact "your own definition layout file is connected to the main layout file, and the database content is looked up and displayed"