小说网站 搜小说 无限网 烟雨红尘 小说爱好者 免费小说 免费小说网站

Android简易实战教程--第十八话《ListView显示,简单的适配器SimpleAdapter》

本篇介绍Listview的显示,对于listview有许多的适配器,如ArrayAdapter,BaseAdapter,SimpleAdapter等等。本篇先热身一下,介绍最简单的SimpleAdapter适配器。

对于安卓界面的显示。

首先在主界面布局文件main.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"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/lv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</RelativeLayout>

只有一个显示数据的组件:ListView。

然后,给ListView的Item定义一个子布局文件。它代表,listview的列表每个条目的布局item_listview.xml。如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    
    <ImageView 
        android:id="@+id/iv_photo"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/photo3"
        />
    <TextView 
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:layout_gravity="center_vertical"
        />

</LinearLayout>
好了,现在就在MainActivity中加入数据显示的代码吧:

package com.itydl.arraysimple;

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

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//获取listview对象
		ListView lv = (ListView) findViewById(R.id.lv);
		
		//集合中每个元素都包含ListView条目需要的所有数据,该案例中每个条目需要一个字符串和一个整型,所以使用一个map来封装这两种数据
		List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();
		//定义三条map信息
		Map<String, Object> map1 = new HashMap<String, Object>();
		map1.put("photo", R.drawable.photo1);
		map1.put("name", "小志的儿子");
		data.add(map1);
		
		Map<String, Object> map2 = new HashMap<String, Object>();
		map2.put("photo", R.drawable.photo2);
		map2.put("name", "小志");
		data.add(map2);
		
		Map<String, Object> map3 = new HashMap<String, Object>();
		map3.put("photo", R.drawable.photo3);
		map3.put("name", "赵帅哥");
		data.add(map3);
		
		lv.setAdapter(new SimpleAdapter(this, data, R.layout.item_listview, 
				new String[]{"photo", "name"}, new int[]{R.id.iv_photo, R.id.tv_name}));//这里注意from和to两个位置要对应
		//就是制定键和值,在布局文件中哪的子节点中显示。不要搞错和搞反了。
	}


}

其中注意一点:就是new SimpleAdapter(this, data, R.layout.item_listview,
                new String[]{"photo", "name"}, new int[]{R.id.iv_photo, R.id.tv_name})

参数含义:上下文,数据源,item的布局文件id,from,to。其中from是一个数组,里面的键,与map的键相同;to也是个数组,表示ite显示的组件id,注意要与from的顺序一致,不然会报错。

好了,现在运行程序,结果如下:


posted on 2016-08-17 18:18  王小航  阅读(133)  评论(0编辑  收藏  举报

导航