ListView学习2

来源:互联网 发布:打火机推荐 知乎 编辑:程序博客网 时间:2024/06/08 19:48

3. 使用SimpleAdapter

使用SimpleAdapter来实现ListView是比较灵活和多变的方法,它所显示的ListView不在仅仅是一行字了,可以是图片、文字、按钮等等。

可以自定义样式,这就意味着需要我们自己来设置布局文件,需要两个布局文件,一个是整个Activity的布局文件,另一个是ListView中每个条目的布局文件,代码片段如下:

Activity布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/layout"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="测试SimpleAdapter"/> <ListView        android:id="@android:id/list"        android:layout_width="fill_parent"        android:layout_height="wrap_content" /></LinearLayout>

 

ListView中每个条目即每行的布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="horizontal">  <!-- 注意每行布局是横向布局 -->    <ImageView android:id="@+id/image"        android:layout_width="wrap_content"        android:layout_height="wrap_content"/>        <TextView android:id="@+id/name"        android:layout_width="180dp"        android:layout_height="30dp"        android:layout_marginLeft="20dp"        android:textColor="#F65327"        android:singleLine="true" />            <TextView android:id="@+id/age"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_marginRight="10dp"        android:gravity="right"        android:textColor="#F65327"/><!-- 最后一个TextView的布局layout_width不用fill_parent的话,就无法右对齐 --></LinearLayout>

JAVA代码:

package com.example.firstandroid;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.ListActivity;import android.os.Bundle;import android.view.View;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.Toast;public class SimpleAdapterDemoActivity extends ListActivity {private ArrayList<Map<String, Object>> list = null;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_simple_adapter_demo);// 创建一个ArrayList,里面的每一项都是一个条目的内容,而每一个条目都是放在HashMap中的键值对list = getData();// SimpleAdapter有若干参数// 1. Context,当前的Activity// 2. 传入的List,List内存放的是Map// 3. 每一个条目的布局文件,即user.xml// 4. 一个String数组,是代表ListView显示的时候,每一列所代表的含义,这个String数组的内容,要与之前Map中放置的键值对的Key相对应// 5. 一个int数组,代表布局文件user.xml中,上面String数组中的Key所对应那个布局SimpleAdapter adapter = new SimpleAdapter(this,list, R.layout.user, new String[] { "image","name", "age" },new int[] { R.id.image, R.id.name, R.id.age });setListAdapter(adapter);}@Overrideprotected void onListItemClick(ListView l, View v, int position, long id) {// super.onListItemClick(l, v, position, id);Toast.makeText(SimpleAdapterDemoActivity.this,String.valueOf(list.get(position).get("name")) + " is " + String.valueOf(list.get(position).get("age"))+ " years old.", Toast.LENGTH_SHORT).show();}public ArrayList<Map<String, Object>> getData() {list = new ArrayList<Map<String, Object>>();HashMap<String, Object> m1 = new HashMap<String, Object>();HashMap<String, Object> m2 = new HashMap<String, Object>();HashMap<String, Object> m3 = new HashMap<String, Object>();m1.put("image", R.drawable.dota1);m1.put("name", "Jack");m1.put("age", "63");m2.put("image", R.drawable.dota1);m2.put("name", "Bob");m2.put("age", "15");m3.put("image", R.drawable.dota2);m3.put("name", "Theron");m3.put("age", "25");list.add(m1);list.add(m2);list.add(m3);return list;}}
 

对上述代码进行分析,首先,它不再继承Activity类,而是继承了ListActivity,需要使用SimpleAdapter来建立ListView的话,需要若干参数。第一个是Activity的上下文对象;第二个是一个List的对象,用来存放数据,List中每一个数据,都是一个Map对象,在Map对象中存放着名称与数据的实际的键值对,上面代码建立了3HashMap,也就是listView中的3个条目,每个条目是一个HashMap,我们的ListView有三列,分别是布局文件中的一个ImageView和两个TextView,因此,每个HashMap中需要设置一个ImageView和两个TextView的数据,设置完成之后,将3HashMap加入List中;第三个参数是每个条目的布局文件,也就是上面的第二个布局文件;第四个参数是一个String数组,表示着我们建立的ListView中每列分别代表什么;第五个参数是一个int数组,代表布局文件user.xml中,上面String数组中的Key所对应那个布局。

接着将SimpleAdapter对象设置给Activity,就可以实现ListView的显示了。

同样,可以onListItemClick方法来对点击事件进行响应。如下为运行效果,可以看出它比上述两种方式更加灵活,可以自由扩展。


 

 

参考:http://theron.blog.51cto.com/2383825/649771