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

时间:2022-11-15 16:49:55 


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>
标签:ui,游标列表
0
投稿

猜你喜欢

  • 基于C语言扫雷游戏的设计与实现

    2023-06-03 01:46:49
  • java实现基于TCP协议网络socket编程(C/S通信)

    2021-10-15 23:41:26
  • OkHttp3中默认不保持Cookie的解决方法

    2021-10-25 15:20:50
  • springboot框架阿里开源低代码工具LowCodeEngine

    2022-09-01 09:40:41
  • Java Swing JComboBox下拉列表框的示例代码

    2022-09-16 00:24:50
  • Unity常用命令模式详解

    2021-12-30 16:58:08
  • Java编程实现对象克隆(复制)代码详解

    2023-02-13 10:12:31
  • C#借助OpenCvSharp读取摄像头并显示的实现示例

    2022-12-22 05:49:51
  • Android中多个ContentProvider的初始化顺序详解

    2021-11-06 03:52:36
  • SpringBoot使用prometheus监控的示例代码

    2021-05-29 02:37:41
  • Spring事务失效的一种原因关于this调用的问题

    2022-07-19 20:59:21
  • Android 中自定义Dialog样式的Activity点击空白处隐藏软键盘功能(dialog不消失)

    2022-03-10 12:15:21
  • C# 创建高精度定时器的示例

    2023-03-09 12:35:58
  • SpringBoot Jpa分页查询配置方式解析

    2023-03-02 10:04:02
  • C#实现视频的批量剪辑功能

    2023-07-15 06:18:30
  • C#数组中List, Dictionary的相互转换问题

    2022-11-03 00:10:37
  • C# 多窗口委托通信的实现

    2023-01-22 02:21:31
  • Java编程将汉字转Unicode码代码示例

    2023-11-09 17:33:39
  • 项目为什么引入log4j而不是logback代码

    2023-02-02 07:51:07
  • c# winform多线程的小例子

    2023-01-05 07:11:44
  • asp之家 软件编程 m.aspxhome.com