ListView是Android 手机系统中使用非常广泛的一种控件,它以垂直列表的形式展示所有列表项。
创建ListView有两种方式:
1)、直接使用ListView进行创建。
2)、让Activity继承ListActivity。
示例1:继承ListActivity创建ListView
XML文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@+id/android:list" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </RelativeLayout>
Activity文件:
public class MainActivity extends ListActivity { String[] str={"刘备","关羽","张飞","赵云"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,str); setListAdapter(adapter); } }
注:
上面的程序中创建了一个ArrayAdapter,创建ArrayAdapter是必须指定一个textViewResourceId,该参数决定每个列表项的外观样式。其中:
Simple_list_item_1:每个类列表项都是一个普通的TextView。
Simple_list_item_2:每个类列表项都是一个普通的TextView(字体略大)。
Simple_list_item_checked:每个列表项都是一个以勾选的列表项。
Simple_list_item_multiple_choice:每个列表项都是带多选框的文本。
Simple_list_item_single_choice:每个列表项都是带多单选按钮的文本。
运行结果:
注:由程序可以看出:ListView的默认布局时由一个位于屏幕中心的列表组成。实际上用户也可以通过setContentView()方法设置。需要注意的是在布局文件中必须包含一耳光id为“@+id/android:list”的ListView。
一旦在程序中获得ListView后就要为它添加相应的列表项。可以使用不同过得方法添加列表项,在下面我们将一一具体介绍。另外ListView其它一些属性如下表所示:
XML属性 | 说明 |
android:choiceMode | 设置ListView的选择行为 |
android:divider | 设置List列表项的分隔条(既可以用颜色分隔,也可以用Drawable分隔) |
android:dividerHeight | 设置分隔条的高度 |
android:entries | 指定一个数组资源,android将根据数组资源来生成ListView |
android:footerDividersEnabled | 如果设置为false,则不再footer View 之前绘制分隔条 |
android:headerDividersEnabled | 如果设置为false,则不再header View 之前绘制分隔条 |
下面将通过具体的例子讲解为ListView添加数据额几种常用的方法
1、 直接使用数组资源给出列表项
2、 使用ArrayAdapter提供ListView的列表项(如示例1.下面就不在给出例子)
3、 使用SimpleAdapter提供ListView的列表项
4、 使用自定义Adapter提供ListView的列表项
5、 使用SimpleCursorAdapter提供ListView的列表项
示例2:通过数组资源直接给出列表项
XMl文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@+id/android:list" android:layout_width="match_parent" android:layout_height="wrap_content" android:entries="@array/renwu" > </ListView> </RelativeLayout>
注:此程序的Activity文件不用做任何修改,默认即可。通过XML代码的11行 android:entries 属性指定列表项内容。因此还要定义一个名为“renwu”数组资源,再此就不在给出代码。上面的程序和示例1运行结果完全相同。
示例3:使用SimpleAdapter提供ListView的列表项
XML文件:
<?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" > <!-- 定义一个List --> <ListView android:id="@+id/mylist" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <!-- 定义一个ImageView,用于作为列表项的一部分。 --> <ImageView android:id="@+id/header" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="10dp" /> <!-- 定义一个TextView,用于作为列表项的一部分。 --> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16dp" android:gravity="center_vertical" android:paddingLeft="10dp" /> </LinearLayout>
Activity文件:
public class SimpleAdapterTest extends Activity { private String[] names = new String[] { "小狗", "小猫", "狮子", "老虎"}; private int[] p_w_picpathIds = new int[] { R.drawable.ic_launcher, R.drawable.ic_launcher , R.drawable.ic_launcher , R.drawable.ic_launcher}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //创建一个List集合,List集合的元素是Map List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>(); for (int i = 0; i < names.length; i++) { Map<String, Object> listItem = new HashMap<String, Object>(); listItem.put("header", p_w_picpathIds[i]); listItem.put("personName", names[i]); listItems.add(listItem); } //创建一个SimpleAdapter SimpleAdapter simpleAdapter = new SimpleAdapter(this , listItems , R.layout.main , new String[]{ "personName", "header" } , new int[]{R.id.name , R.id.header}); ListView list = (ListView)findViewById(R.id.mylist); //为ListView设置Adapter list.setAdapter(simpleAdapter); } }
注:
使用SimpleAdapter的最大难点在于创建SimpleAdapter对象,它需要5个参数,其中后面4个参数比较重要。
第二个参数:该参数是一个List<?extends Map<String ,?>>类型的集合对象,该集合中的每个Map<String,?>对象生成一个列表项。
第三个参数:该参数指定一个界面布局ID。也就是第五个参数中界面组件就来自该界面布局。
第四个参数:该参数是一个String[] 类型的参数,该参数决定提取Map<Sreing ,?>对象中那些key对应的value来生成列表项。
第五个参数:该参数是一个int[]类型的参数,该参数决定使用哪些View组件来合成一个列表项。
运行结果:
使用自定义Adapter和SimpleCursorAdapter提供ListView的列表项用法比较复杂。将在下面的博客的中介绍。