vListView是手机系统中使用非常广泛的一种组件,它以垂直列表的形式显示所有列表项,创建ListView有两种方式。
根据列表的适配器类型,列表分为三种,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter:
ArrayAdapter最为简单,只能展示一行字。
SimpleAdapter有最好的扩充性,可以自定义出各种效果。
SimpleCursorAdapter可以认为是SimpleAdapter对数据库的简单结合,可以方面的把数据库的内容以列表的形式展示出来。
simpleAdapter
的扩展性最好,可以定义各种各样的布局出来,可以放上ImageView(图片),还可以放上Button(按钮),CheckBox(复选框)等等。
使用simpleAdapter的数据用一般都是HashMap构成的List,list的每一节对应ListView的每一行。
HashMap的每个键值数据映射到布局文件中对应id的组件上。因为系统没有对应的布局文件可用,我们可以自己定义一个布局*.xml。
有时候,列表不光会用来做显示用,我们同样可以在在上面添加按钮。但是按钮是无法映射的,
即使你成功的用布局文件显示出了按钮也无法添加按钮的响应,这时就要研究一下ListView是如何现实的了,而且必须要重写一个类继承BaseAdapter。
SimpleCursorAdapter
简单的说就是方便把从游标得到的数据进行列表显示,并可以把指定的列映射到对应的TextView中。
startManagingCursor(cursor);我们将获得的Cursor对象交由Activity管理,这样Cursor的生命周期和Activity便能够自动同步,省去自己手动管理Cursor。
布局文件:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <ListView
- android:id="@+id/list2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:divider="@drawable/line">
- </ListView>
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="2px"
- android:background="#ff0000"
- />
- <ListView
- android:id="@+id/list3"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:divider="@drawable/line">
- </ListView>
- </LinearLayout>
列表布局文件:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:id="@+id/list_name"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <TextView
- android:id="@+id/list_mes"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
java文件:列表里可以添加按钮图片
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- import android.widget.SimpleAdapter;
- public class AndroidtestActivity15 extends Activity{
- private ListView list2;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main15);
- //************************用ArrayAdapter方法添加listview***************************************//
- //定义一个数组
- String[] arr={"孙悟空","猪八戒","牛魔王"};
- //将数组包装arrayadapter android.R.layout.simple_list_item_1
- ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,arr);
- list2=(ListView) findViewById(R.id.list2);
- list2.setTextFilterEnabled(true);
- //给list2设置adapter
- list2.setAdapter(arrayAdapter);
- //************************用SimpleAdapter方法添加listview***************************************//
- List<Map<String, String>> list = new ArrayList<Map<String,String>>();
- Map<String, String> date1 = new HashMap<String, String>();
- date1.put("name", "java");
- date1.put("mess", "是基础");
- Map<String, String> date2= new HashMap<String, String>();
- date2.put("name", "android");
- date2.put("mess", "j2me");
- Map<String, String> date3= new HashMap<String, String>();
- date3.put("name", "jsp");
- date3.put("mess", "j2ee,web方向");
- list.add(date1);
- list.add(date2);
- list.add(date3);
- SimpleAdapter simpleAdapter=new SimpleAdapter(this,
- list,
- R.layout.main15_2,
- new String[]{"name","mess"},
- new int[]{R.id.list_name,R.id.list_mes});
- ListView list3=(ListView) findViewById(R.id.list3);
- list3.setAdapter(simpleAdapter);
- }
- }