Android List Implementation (2) _ cursor list Case Study

Source: Internet
Author: User

Copy codeThe Code is 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
* Data comes from a cursor, and
* SimpleCursorListAdapter is used to map each item to a two-line
* Display.
*/
Public class List3 extends ListActivity {

@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );

// Get a cursor with all phones
Cursor c = getContentResolver (). query (Phones. CONTENT_URI, 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: In this example, you must grant permissions to the program.:Copy codeThe Code is as follows: <uses-permission android: name = "android. permission. READ_CONTACTS"/>

Simple_list_item_2.xmlCopy codeThe Code is 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 may not use this file before t in compliance with the License.
You may obtain a copy of the License

Http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
Distributed under the License is distributed on an "as is" BASIS,
Without warranties or conditions of any kind, either express or implied.
See 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.