Copy Code code as follows:
Import android.app.ListActivity;
Import Android.database.Cursor;
Import Android.os.Bundle;
Import Android.provider.Contacts.Phones;
Import Android.widget.ListAdapter;
Import Android.widget.SimpleCursorAdapter;
/**
* A List View example where the
* Data comes from a cursor, and a
* Simplecursorlistadapter is used to map all item to a two-line
* Display.
*/
public class List3 extends Listactivity {
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Get a cursor and all phones
Cursor C = getcontentresolver (). query (Phones.content_uri, NULL, NULL, NULL, NULL);
Startmanagingcursor (c);
Map Cursor columns to views defined in Simple_list_item_2.xml
ListAdapter adapter = new Simplecursoradapter (This,
Android. R.layout.simple_list_item_2, C,
New string[] {phones.name, phones.number},
New int[] {Android. R.id.text1, Android. R.ID.TEXT2});
Setlistadapter (adapter);
}
}
Note that this example gives the program permission to:
Copy Code code as follows:
<uses-permission android:name= "Android.permission.READ_CONTACTS"/>
Simple_list_item_2.xml
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<!--Copyright (C) 2006 the Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
You are not to use this file except in compliance with the License.
You could obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable or agreed to in writing, software
Distributed under the License is distributed on ' as is ' basis,
Without warranties or CONDITIONS of any KIND, either express or implied.
The License for the specific language governing permissions and
Limitations under the License.
-->
<twolinelistitem xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:minheight= "Android:attr/listpreferreditemheight"?
Android:mode= "Twoline"
>
<textview android:id= "@android: Id/text1"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:layout_marginleft= "Android:attr/listpreferreditempaddingleft"?
android:layout_margintop= "8dip"
Android:textappearance= "Android:attr/textappearancelistitem"?
/>
<textview android:id= "@android: Id/text2"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:layout_below= "@android: Id/text1"
Android:layout_alignleft= "@android: Id/text1"
Android:textappearance= "Android:attr/textappearancesmall"?
/>
</TwoLineListItem>