Android常见控件之SimpleAdapter和ListView

一、SimpleAdapter

      SimpleAdapter是一个简单的适配器,可以将静态数据映射到XML文件中定义好的视图。你可以指定数据支持的列表如ArrayList组成的Map。在ArrayList中的每个条目对应List中的一行。Maps包含每行数据。你可以指定一个定义了被用于显示行的视图XML文件,通过关键字映射到指定的视图。

     构造函数

    public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)

    参数

          context     关联SimpleAdapter运行着的视图的上下文。

          data         一个Map的列表。在列表中的每个条目对应列表中的一行,应该包含所有在from中指定的条目

          resource  一个定义列表项目的视图布局的资源唯一标识。布局文件将至少应包含哪些在to中定义了的名称。

          from         一个将被添加到Map上关联每一个项目的列名称的列表

          to             应该在参数from显示列的视图。这些应该全是TextView。在列表中最初的N视图是从参数from中最初的N列获取的值。

      一个SimlpleAdapter是这样工作的。假设将SimpleAdapter用于ListView。那么ListView的每一个列表项就是 resource参数值指定的布局。而data参数就是要加载到ListView中的数据。我们先看每一个列表项,假设列表项所对应的布局文件中包含了两个组件:TextView和EditText,id分别为textview和edittext。那么在加载列表项时,需要通过组件的id和data参数中 List元素中的Map对象对应。因此,from参数Map对象的key,而to表示组件的id,例如,本例中的参数值为from = new String[]{"userId", "userName"},to = new int[]{R.id.userId,R.id.userName}。意思就是将Map对象中key为userId的value绑定到 R.id.userId上,userName也类似。现在来看data参数,一个ListView由多个列表项组成。每一个列表项由一个Map对象提供数据,而多个列表项则由List对象提供多个 Map对象。

二、ListView

    在android开发中ListView是比较常用的组件,它以列表的形式展示具体内容,并且能够根据数据的长度自适应显示。

      列表的显示需要三个元素:

           1.ListVeiw 用来展示列表的View。

           2.适配器 用来把数据映射到ListView上的中介。

           3.数据 具体的将被映射的字符串,图片,或者基本组件。

      根据列表的适配器类型,列表分为三种,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter。其中以ArrayAdapter最为简单,只能展示一行字。SimpleAdapter有最好的扩充性,可以自定义出各种效果。SimpleCursorAdapter可以认为是SimpleAdapter对数据库的简单结合,可以方面的把数据库的内容以列表的形式展示出来。SimpleAdapter继承自AdapterView。我们可以通过setOnItemClickListener()方法给ListView添加监听器,当用户点击某一个列表项中执行相应的操作。在监听器中需要复写public abstract void onItemClick (AdapterView<?> parent, View view, int position, long id)方法。

      如果需要访问与被选项相关的数据,执行程序可以调用getItemAtPosition(position)。

      参数

      parent  发生点击动作的AdapterView。

      view 在AdapterView中被点击的视图(它是由adapter提供的一个视图)。

      position 视图在adapter中的位置。

      id 被点击元素的行id。

      示例:我们一SimpleAdapter为例说明ListView的用法。

Android_ListView.java

       package com.idea.org; import java.util.ArrayList; import java.util.HashMap; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Toast; public class Android_ListView extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ArrayList<HashMap<String,String>>list =new ArrayList<HashMap<String,String>>(); HashMap<String,String> map1=new HashMap<String,String>(); HashMap<String,String> map2=new HashMap<String,String>(); HashMap<String,String> map3=new HashMap<String,String>(); ListView listView=(ListView)findViewById(R.id.listView); map1.put("userId", "100001"); map1.put("userName", "用户一"); list.add(map1); map2.put("userId", "100002"); map2.put("userName", "用户二"); list.add(map2); map3.put("userId", "100003"); map3.put("userName", "用户三"); list.add(map3); //定义一个SimpleAdapter,每一个行有两个TextView,分别显示userId和userName SimpleAdapter simpleAdapter=new SimpleAdapter(this,list,R.layout.user, new String[]{"userId","userName"},new int[]{R.id.userId,R.id.userName}); //为ListView添加适配器 listView.setAdapter(simpleAdapter);//设置listView背后的数据为simpleAdapter。 /*为listView添加单击监听器,需要import android.widget.AdapterView.OnItemClickListener;语句 * 当点击某一个列表项时,用Toast显示这个列表项中的文字内容 */ listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { ListView listView=(ListView)arg0; Toast.makeText(Android_ListView.this, listView.getItemAtPosition(arg2).toString(), Toast.LENGTH_SHORT).show(); } }); } }

main.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="wrap_content" > <!--添加一个ListView控件 --> <ListView android:id="@+id/listView" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>

SimpleAdapter所用的布局文件user.xml

       <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingRight="10dip" android:paddingLeft="10dip" android:paddingTop="1dip" android:paddingBottom="1dip" > <!--ListView的每一项都有两个TextView,分别显示userId和userName --> <TextView android:id="@+id/userId" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="20pt" android:layout_weight="1" /> <TextView android:id="@+id/userName" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="20pt" android:layout_weight="1" /> </LinearLayout>

      运行效果

      Android常见控件之SimpleAdapter和ListView_第1张图片

      Android常见控件之SimpleAdapter和ListView_第2张图片

      奇怪的是ListView无法充满Android3.0模拟器的整个屏幕,不知道是由于Android3.0版本的问题,还是其它的什么原因。

Android常见控件之SimpleAdapter和ListView_第3张图片

你可能感兴趣的:(android,ListView,String,list,layout,encoding)