ListView 初见

ListView 的用法

android 的 ListView 常用于组织若干个同类的view。它的数据是由适配器提供,适配器相当与在真实数据上做了一层针对具体 ListView 的封装。通过适配器,可以实现子view的添加,删除。

ListView不能直接添加view,必须通过适配器。如果直接调用 addView 方法会报错:

addView(View) is not supported in AdapterView

可选的适配器有三种,分别是 ArrayAdapter,SimpleAdapter,SimpleCursorAdapter

ArrayAdapter

ArrayAdapter 用于显示文字。效果如下:

* java中为ListView设置适配器。
ArrayAdapter还有其他的构造函数 ArrayAdapter 文档

   ListView demos = (ListView)findViewById(R.id.demos);
   
   List<String> cities = new ArrayList<String>();
   cities.add("北京");
   cities.add("上海");
   cities.add("广州");
   cities.add("深圳");

   demos.setAdapter(new ArrayAdapter<String>(this, R.layout.demo_name,cities));

R.layout.demo_name 是一个独立的布局文件,只包含一个 TextView。这个TextView就是ListView中每行显示的元素。

  • 主布局文件中添加 ListView
    <ListView
        android:id="@+id/demos"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </ListView>
  • 新建布局文件展示列表元素,布局文件仅包含一个 TextView
<TextView"/>

以上是简略的代码,只摘出了关键的部分。

SimpleAdapter

SimpleAdapter 比 ArrayAdapter 要强大很多,可以显示 CheckBox,TextView,ImageView。
主布局文件中的 ListView 和上面用到的一样,就不贴代码了。主要是适配器和行列表元素对应的布局文件。

  • 行布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <TextView
        android:id="@+id/name"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:textStyle="normal"
        style="@style/MainText"
        />

    <ImageView
        android:id="@+id/photo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="centerInside"
        android:layout_toRightOf="@id/name"/>

</RelativeLayout>

注意布局文件中 TextView的 id 为 name, ImageView 的 id 为 photo。这个会在定义适配器的时候用到。

  • 定义适配器
  ListView listviewImg = (ListView)findViewById(R.id.listview_img);
  List<Map<String,Object>> imgData = new ArrayList<Map<String,Object>>();
  Map<String, Object> namei = new HashMap<String, Object>();
  namei.put("name","娜美");
  namei.put("photo",R.drawable.namei_1);
  Map<String, Object> luobin = new HashMap<String, Object>();
  luobin.put("name","妮可罗宾");
  luobin.put("photo",R.drawable.luobin_1);
  imgData.add(namei);
  imgData.add(luobin);

  listviewImg.setAdapter(new SimpleAdapter(this, imgData,R.layout.layout_imgs, new String[]{"name","photo"}, new int[]{R.id.name,R.id.photo}));

1 注意这里的数据类型,和 ArrayAdapter 所依赖的有很大区别。因为 SimpleAdapter 所要支持的 view 不止一种,TextView需要String类型的数据,而ImageView可以使用resource id,等等,所以这里Map的key可以为任何类型(Object)。

 List<Map<String,Object>> imgData = new ArrayList<Map<String,Object>>();

2 定义适配器的代码,new String[]{"name","photo"} 代表 from。new int[]{R.id.name,R.id.photo} 代表 to。
官网对这两个参数的解释如下:

from @String: A list of column names that will be added to the Map associated with each item.

to @int: The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter.

每一个列表项称为一行,每一行中有若干列。
from 中定义是 imgData 中每一个 map 的key,to 中定义的是对应前面 map 中 key 的 view id。简单来说,一个 key 对应一个view,key的value会渲染到view上。也就是说,name 的值会渲染到id为name的textview上,photo的值会渲染到id为photo的imageview上。

效果图

ps:顶栏是重新做的一个 toolbar,和上面代码无关。

posted @ 2016-06-23 11:16  六月的海  阅读(182)  评论(0编辑  收藏  举报