欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android列表实现(2)_游标列表案例讲解

程序员文章站 2023-12-14 18:09:58
复制代码 代码如下:import android.app.listactivity; import android.database.cursor; import andr...
复制代码 代码如下:

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 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, 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);
}

}

注意 该例子要给程序赋予权限
复制代码 代码如下:

<uses-permission android:name="android.permission.read_contacts"/>

simple_list_item_2.xml
复制代码 代码如下:

<?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 except in compliance with the license.
you may obtain a copy of the license at

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>

上一篇:

下一篇: