Android list Implementation (2) _ Cursor list case explanation _android

Source: Internet
Author: User
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>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.