ListView用来罗列数据,样式也可以像表格一样,SimpleAdapter用于把数据存放在ListView中。
1、ListView的实现主要依靠配置文件
首先是主Activity中要配置要显示的ListView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fcfcfc"
android:orientation="vertical" >
<ListView
android:id="@+id/showListView"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:fadingEdge="none"
android:scrollingCache="false" />
</LinearLayout>
第二,独立一个显示ListView内容格式的xml文件,有多少列就有多少个textview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:paddingBottom="3dip"
android:paddingLeft="10dip" >
<TextView
android:id="@+id/time"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:textSize="15dip" />
<TextView
android:id="@+id/shop"
android:layout_width="95dp"
android:layout_height="wrap_content"
android:layout_weight="5.43"
android:textSize="15dip" />
<TextView
android:id="@+id/money"
android:layout_width="72dp"
android:layout_height="wrap_content"
android:textSize="15dip" />
</LinearLayout>
第三,SimpleAdapter出场
SimpleAdapter simpleAdapter = new SimpleAdapter(QueryTradeActivity.this,
info,R.layout.tradelist,new String[]{"time","shop","money"},new int[]{R.id.time,R.id.shop,R.id.money});
第一个参数为当前activity;二个参数为 List<Map<String, String>>格式的数据;第三个参数为定义listview内容的xml文件(上文第二步);第四个参数为要显示的列,字符串内容对应info数据中的键名;第五个参数为对应定义listview内容的xml文件中定义的textview id。