Android 之 ListView列表控件 的SimpleAdapter适配器


ListView用法介绍:
列表的显示需要三个元素:
1、ListView 这个组件,用于显示;
2、适配器  用于绑定数据,就是讲数据映射到ListView上;
3、数据  需要映射到ListView的数据,可以是字符串 图片 或者基本的组件;

知识点2:适配器的类型
根据列表的适配器类型,列表分为ArrayAdapter,SimpleAdapter和SimpleCursorAdapter
其中以ArrayAdapter最为简单,只能显示一行字,SimpleAdapter有最好的扩充性,可以定义出
各种效过.SimpleCursorAdapter可以认为是SimpleAdapter对数据库的简单结合,可以方便的
把数据库的内容以列表的形式展示出来。

SimpleCursorAdapter:
sdk的帮助文档对该适配器的解释为: An easy adapter to map columns from a
cursor to TextViews or ImageViews defined in an xml file.You
can specify which columns you want , which views you want to display
the columns ,and the xml file that defines the appearance of these
views;
简单的说就是方便把从游标得到的数据进行列表显示,并可以把指定的列映射到对应的TextView中。


SimpleAdapter:
SimpleAdapter 的扩展性最好,可以定义各种各样的布局出来,可以放ImageView(图片),还可以放Button
CheckVox 等等;
使用时:直接继承ListActivity , ListActivity和普通的Activity没有太大的差别,不同的就是
对显示ListView做了许多优化,方便显示而已;

案例:程序实现一个带有图片的列表。

		用于显示在列表上的的xml布局代码如下:
		<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <!-- 用于显示在列表中每一列的xml布局文件 -->
    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
       
            android:textSize="22px" />

        <TextView
            android:id="@+id/info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
         
            android:textSize="22px" />
    </LinearLayout>

</LinearLayout>


java程序代码如下:
		package com.example.listview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {

	private ListView listView;
	private List<String> dataList = new ArrayList<String>();
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		listView = new ListView(this);
		
		SimpleAdapter adapter = new SimpleAdapter(this, getDate(), R.layout.main, new String[]{"title","info","img"}, new int[]{R.id.title,R.id.info,R.id.img});
		listView.setAdapter(adapter);
		
		setContentView(listView);
		

	}
	
	/* M 实现数据源 */
	private ArrayList<Map<String, Object>> getDate(){
		ArrayList<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("title", "G1");
		map.put("info", "google1");
		map.put("img", R.drawable.ic_launcher);
		list.add(map);
		
		/* 重写实例化, 获取第二批数据 */
		map = new HashMap<String, Object>();
		map.put("title", "G2");
		map.put("info", "google2");
		map.put("img", R.drawable.ic_launcher);
		list.add(map);
		
		/* 重写实例化, 获取第二批数据 */
		map = new HashMap<String, Object>();
		map.put("title", "G3");
		map.put("info", "google3");
		map.put("img", R.drawable.ic_launcher);
		list.add(map);
		
		return list;
	}
}



小贴士:
使用SimpleAdapter的数据一般都是HashMap构成的List,list的每一节对应
ListView的每一行。HashMap的每个键值数据映射到布局文件中对应id的组件上。
由于系统没有可对应的布局文件使用,那么我们可以自己定义一个布局main.xml;
适配过程:new一个SimpleAdapter对象;
参数1:this;
参数2:数据
参数3;布局文件,main.xml用于显示在列表行上的布局文件;
参数4:HashMap的title info img ;
参数5:布局文件的组件id; title info img




猜你喜欢

转载自sunzone.iteye.com/blog/1871111