ListActivity是继承自Activity的一个子类,这个ListActivity可以用来展示一个列表数据其数据源可以绑定到一个数组或者是数据库游标。
在进行列表数据展示时可以直接让自己的Activity继承自ListActivity ,ListActivity 自己有一个默认持有一个ListView控件,也就是说你不用在LayOut布局文件中放置一个ListView 的配置节。
ListActivity 默认持有的这个ListView是一个全屏的居中显示的列表控件。在OnCreate 方法中不需要通过setContentView()来设置主界面的显示LayOut布局文件,也就是说在默认情况下是 不需要一个XML文件来作为ListActivity的界面展现,其实是系统替咱们做了。
下面这个例子里面我只是定义了一个用来呈现列表项
内容的xml布局,并不包含ListActivity的界面布局。
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="horizontal" > 6 7 <ImageView 8 android:id="@+id/list_item_image" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" /> 11 12 <LinearLayout 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:orientation="vertical" > 16 <TextView 17 android:id="@+id/list_item_title" 18 android:layout_width="wrap_content" 19 android:layout_height="wrap_content" 20 /> 21 <TextView 22 android:id="@+id/list_item_info" 23 android:layout_width="wrap_content" 24 android:layout_height="wrap_content" 25 /> 26 </LinearLayout> 27 28 </LinearLayout>
package com.sane.activity; import com.sane.androidsearchviewdemo.R; import android.app.ListActivity; import android.os.Bundle; import android.widget.ListView; import android.widget.SimpleAdapter; public class SimpleAdapterDemo extends ListActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String[] from = { "title", "info", "image" }; // 数据源 键 数组 int[] to = { R.id.list_item_title, R.id.list_item_info, R.id.list_item_image };// 用来显示数据的列表项内布局文件的组件数组 /** * from 数组的数据会对应显示到to数组的控件之上。 */ SimpleAdapter simpleAdapter = new SimpleAdapter(this, DataPool.getListData(), R.layout.list_item_layout, from, to); setListAdapter(simpleAdapter); } }
package com.sane.activity; import java.util.ArrayList; import java.util.HashMap; import com.sane.androidsearchviewdemo.R; public class DataPool { private static ArrayList<HashMap<String, Object>> mapDatas; public static ArrayList<HashMap<String, Object>> getListData() { if (mapDatas == null) { mapDatas = new ArrayList<HashMap<String, Object>>(); HashMap<String, Object> map1 = new HashMap<String, Object>(); map1.put("title", "jay"); map1.put("info", "2000年11月7日"); map1.put("image", R.drawable.jay_1); HashMap<String, Object> map2 = new HashMap<String, Object>(); map2.put("title", "范特西"); map2.put("info", "2001年9月20日"); map2.put("image", R.drawable.jay_2); HashMap<String, Object> map3 = new HashMap<String, Object>(); map3.put("title", "八度空间"); map3.put("info", "2002年7月19日"); map3.put("image", R.drawable.jay_3); HashMap<String, Object> map4 = new HashMap<String, Object>(); map4.put("title", "叶惠美"); map4.put("info", "2003年7月31日"); map4.put("image", R.drawable.jay_4); HashMap<String, Object> map5 = new HashMap<String, Object>(); map5.put("title", "七里香"); map5.put("info", "2004年8月3日"); map5.put("image", R.drawable.jay_5); HashMap<String, Object> map6 = new HashMap<String, Object>(); map6.put("title", "十一月的萧邦"); map6.put("info", "2005年11月1日"); map6.put("image", R.drawable.jay_6); HashMap<String, Object> map7 = new HashMap<String, Object>(); map7.put("title", "依然范特西"); map7.put("info", "2006年9月5日"); map7.put("image", R.drawable.jay_7); HashMap<String, Object> map8 = new HashMap<String, Object>(); map8.put("title", "我很忙"); map8.put("info", "2007年11月2日"); map8.put("image", R.drawable.jay_8); HashMap<String, Object> map9 = new HashMap<String, Object>(); map9.put("title", "魔杰座"); map9.put("info", "2008年10月15日"); map9.put("image", R.drawable.jay_9); HashMap<String, Object> map10 = new HashMap<String, Object>(); map10.put("title", "跨时代"); map10.put("info", "2010年5月18日"); map10.put("image", R.drawable.jay_10); HashMap<String, Object> map11 = new HashMap<String, Object>(); map11.put("title", "惊叹号"); map11.put("info", "2011年11月11"); map11.put("image", R.drawable.jay_11); HashMap<String, Object> map12 = new HashMap<String, Object>(); map12.put("title", "12新作"); map12.put("info", "2013年5月22日"); map12.put("image", R.drawable.jay_12); HashMap<String, Object> map13 = new HashMap<String, Object>(); map13.put("title", "哎呦,不错哦"); map13.put("info", "2014年12月5"); map13.put("image", R.drawable.jay_13); mapDatas.add(map1); mapDatas.add(map2); mapDatas.add(map3); mapDatas.add(map4); mapDatas.add(map5); mapDatas.add(map6); mapDatas.add(map7); mapDatas.add(map8); mapDatas.add(map9); mapDatas.add(map10); mapDatas.add(map11); mapDatas.add(map12); mapDatas.add(map13); } return mapDatas; } }
运行效果图:
其实也可以自己定制存放ListView 所在的界面布局,只不过这里有要求就是在使用自己创建的listview风格的时候ListActivity中必须有一个ListView,因为ListActivity启动时会默认去寻找这个ListView的id,如果没有找到,就会报错。解决的方法很简单,就是在主Activity布局文件中加个ListView,且ID为list。
在这里还可以放置一个名字为empty 的控件 或者是布局,被命名为empty的控件布局会在ListView的列表为空的时候显示出来。下面是我的效果图,在ListActivity所使用的布局界面中我放置了一个标题TextView 和一个empty 的textView其运行效果分别如下。
"周杰伦历年专辑列表"是一个textView
数据列表为空的时候显示empty
代码:
1 package com.sane.activity; 2 3 import com.sane.androidsearchviewdemo.R; 4 5 import android.app.ListActivity; 6 import android.os.Bundle; 7 import android.widget.ListView; 8 import android.widget.SimpleAdapter; 9 10 public class SimpleAdapterDemo extends ListActivity { 11 12 @Override 13 protected void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 setContentView(R.layout.list_activity_layout);//在自己定义布局文件时就加这一句 16 String[] from = { "title", "info", "image" }; // 数据源 键 数组 17 int[] to = { R.id.list_item_title, R.id.list_item_info, 18 R.id.list_item_image };// 用来显示数据的列表项内布局文件的组件数组 19 /** 20 * from 数组的数据会对应显示到to数组的控件之上。 21 */ 22 SimpleAdapter simpleAdapter = new SimpleAdapter(this, 23 DataPool.getListData(), R.layout.list_item_layout, from, to); 24 setListAdapter(simpleAdapter); 25 } 26 27 }
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:orientation="vertical" >
6 7 <TextView 8 android:layout_width="match_parent" 9 android:layout_height="wrap_content" 10 android:gravity="center_horizontal" 11 android:background="#F8F8FF" 12 android:paddingBottom="10dip" 13 android:paddingTop="10dip" 14 android:text="@string/headview_text" 15 16 android:textSize="20sp" /> 17 18 <ListView 19 android:id="@android:id/list" 20 android:layout_width="match_parent" 21 android:layout_height="0dip" 22 android:drawSelectorOnTop="false" 23 android:background="#F8F8FF" 24 android:layout_weight="1" > 25 </ListView> 26 27 <TextView 28 android:id="@android:id/empty" 29 android:layout_width="match_parent" 30 android:layout_height="match_parent" 31 android:gravity="center" 32 android:background="#F8F8FF" 33 android:textSize="20sp" 34 android:text="@string/list_no_data" /> 35 36 </LinearLayout>