Android ListView 列表控件的简单使用


ListView 列表是我们经常会使用的控件, 如果想要自定义里面的显示的话是挺麻烦的, 需要新建XML、Class SimpleAdapter这两个文件, 较为麻烦。 如果我们只是想显示两、三行文字在上面, 却又不想那么麻烦呢? 那我们只要新建一个XML就够了。

这里以显示一个ListView项里三个TextView为例。

首先我们要创建一个XML文件, 这个XML文件是用来作为单个ListView项布局用的。

list_row.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <RelativeLayout xmlns:Android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:background="#ffffff"  
  6.     >  
  7.   
  8.     <TextView  
  9.         android:id="@+id/textTo"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:textSize="16dip"  
  13.         android:textColor="#333333"  
  14.         />      
  15.           
  16.     <TextView  
  17.         android:id="@+id/textOwn"  
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="wrap_content"  
  20.         android:layout_below="@id/textTo"  
  21.         android:textSize="12dip"  
  22.         android:textColor="#999999"  
  23.         />  
  24.       
  25.     <TextView  
  26.         android:id="@+id/textState"  
  27.         android:layout_width="wrap_content"  
  28.         android:layout_height="wrap_content"  
  29.         android:layout_alignParentRight="true"  
  30.         android:textSize="14dip"  
  31.         android:textColor="#999999"  
  32.         />  
  33. </RelativeLayout>  

第一个TextView是标题、第二个是内容、第三个是状态

接下来我们需要在主XML布局文件里面放置一个ListView控件

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical"  
  6.     android:background="#ffffff"  
  7.      >  
  8.   
  9.     <ListView   
  10.         android:id="@+id/list"  
  11.         android:layout_width="fill_parent"  
  12.         android:layout_height="fill_parent"  
  13.         android:background="#ffffff"  
  14.         ></ListView>  
  15.   
  16. </LinearLayout>  

然后,我们要在主Activity里面声明三个成员变量

  1. private List<Map<String, Object>> mList;      
  2. private ListView mListView;  
  3. private SimpleAdapter mListAdapter;   

mList是用来存放要显示的数据

SimpleAdapter是ListView 数据的一个容器, 用来存放显示在ListView上的数据。 对 SimpleAdapter 的数据操作会直接影响到ListView的显示。

然后, 我们来给mList添加一些要显示的数据

  1. mList  = new ArrayList<Map<String,Object>>();  
  1. Map<String, Object> map = new HashMap<String, Object>();  
  2. map.put("First""这是标题");  
  3. map.put("Next",  "这是内容");  
  4. map.put("State""状态");  
  5.   
  6. mList.add(map);  

这样就添加了一条数据, 如果要添加多条就重复再添加。

接下来我们把数据放入到SimpleAdapter/ListView中

  1. mListAdapter = null;  
  2. mListAdapter = new SimpleAdapter(this, mList, R.layout.list_row,  
  3. new String[]{"First""Next""State"},  
  4. new int[]{R.id.textOwn, R.id.textTo, R.id.textState});  
  5.   
  6. mListView.setAdapter(mListAdapter);  

new SimpleAdapter的参数: 父指针、ArrayList的数据、 布局文件、 要显示的数据的标签、显示在哪些控件上。  后面两个参数顺序一定要对应。

最后, ListView载入了SimpleAdapter就可以了。

当然,我们直接操作mList也会影响到ListView的数据。 在修改了mList的数据后,调用SimpleAdapter的notifyDataSetChanged()方法后就可以了。

更多Android相关信息见Android 专题页面 http://www.bkjia.com/topicnews.aspx?tid=11

相关内容