Android编程使用ListView实现数据列表显示的方法

2019-12-10 19:31:13于丽
易采站长站为您分析Android编程使用ListView实现数据列表显示的方法,实例分析了Android中ListView控件的使用技巧,需要的朋友可以参考下  

本文实例讲述了Android编程使用ListView实现数据列表显示的方法。,具体如下:

要将数据库中的数据列表显示在屏幕上,我们要使用ListView这个控件,当用户从数据库中取出数据时,要将数据绑定到显示控件上,如何绑定呢,我们需要创建适配器进行绑定,创建适配器有两种方式:

第一种是用SimpleAdapter创建(要求绑定的数据是List<HashMap<String, Object>>数据类型)

第二种是用SimpleCursorAdapter创建(要求绑定的数据是Cursor数据类型)

显示效果如图所示:

Android编程使用ListView实现数据列表显示的方法

界面布局:

item.xml

<?xml version="1.0" encoding="utf-8"?>
<!--item -->
<LinearLayout
 xmlns:android="http://www.easck.com/apk/res/android"
 android:orientation="horizontal"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <!-- 名称 -->
 <TextView
  android:layout_width="130dp"
  android:layout_height="wrap_content"
  android:id="@+id/name"
 />
 <!-- 电话 -->
 <TextView
  android:layout_width="150dp"
  android:layout_height="wrap_content"
  android:id="@+id/phone"
 />
 <!-- 存款 -->
 <TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:id="@+id/amount"
 />
</LinearLayout>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://www.easck.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
 <!-- 标题 -->
 <LinearLayout
 android:orientation="horizontal"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content">
 <TextView
  android:layout_width="130dp"
  android:layout_height="wrap_content"
  android:text="姓名"
 />
  <TextView
  android:layout_width="150dp"
  android:layout_height="wrap_content"
  android:text="电话"
 />
 <TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="存款"
 />
</LinearLayout>
 <!-- ListView控件 -->
<ListView
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:id="@+id/listView"
  />
</LinearLayout>