您的位置:首页 > 移动开发 > Android开发

android ListView适配器之SimpleAdapter的用法

2015-04-30 13:44 501 查看
以前写过ListView的适配器中最简单的ArrayAdapter这个适配器,当listView中只需要显示一个数据时,使用ArrayAdapter适配器很方便,但是如果要向listview的每一行显示多行数据时,ArrayAdapter就不能满足需求了。这个时候SimpleAdapter就派上用场了,SimpleAdapter可以让ListView的每一行显示多项数据,图文并茂等。

使用simpleAdapter的数据用一般都是HashMap构成的List,list的每一节对应ListView的每一行。HashMap的每个键值数据映射到布局文件中对应id的组件上。

构造函数:

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


参数:context  SimpleAdapter关联的View的运行环境

data    一个Map组成的List。在列表中的每个条目对应列表中的一行,每一个map中应该包含所有在from参数中指定的键

resource 一个定义列表项的布局文件的资源ID。布局文件将至少应包含那些在to中定义了的ID

from 一个将被添加到Map映射上的键名

to     将绑定数据的视图的ID,跟from参数对

下面就举个例子,要显示在listView的一行中显示一个人的信息。

主布局文件

<?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="match_parent"
android:background="@drawable/background"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="30sp"
android:textColor="#ffffff"
android:text="病人一般信息"/>
<ListView
android:id="@+id/lv01"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</LinearLayout>
listView的item

<?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="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/question"
android:layout_width="0sp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="25sp"/>
<TextView
android:id="@+id/answer"
android:layout_width="0sp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="25sp"/>

</LinearLayout>


在ListView的每一行中显示question和answer两项数据,首先要准备question和answer两个数组,然后通过map将他们一一对应的添加到liStView的每一行中去。

package com.example.project_simpleadapter;

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

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {
private ListView listView;

private String []  question= new String[]{"日期:","姓名:","性别:","年龄:","联系电话:"};
private String []  answer= new String[]{"2015.4.30","张三","男","58","15254537894"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

listView = (ListView) findViewById(R.id.lv01);
List<Map<String,Object>> mData= new ArrayList<Map<String,Object>>();;
for(int i =0; i < answer.length; i++) {
Map<String,Object> item = new HashMap<String,Object>();
item.put("question", question[i]);
item.put("answer", answer[i]);

mData.add(item);
}
SimpleAdapter adapter = new SimpleAdapter(this,mData,R.layout.item,new String[]{"question","answer"},new int[]{R.id.question,R.id.answer});
listView.setAdapter(adapter);
}
}
完成的效果。



如果要显示图片和文字的话,就需要准备一系列的图片文件,然后修改item,在item中添加一个image

item.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="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/question"
android:layout_width="0sp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="25sp"/>
<TextView
android:id="@+id/answer"
android:layout_width="0sp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="25sp"/>

</LinearLayout>
实现类

package com.example.project_simpleadapter;

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

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {
private ListView listView;

private int [] images = new int []{R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};
private String []  question= new String[]{"日期:","姓名:","性别:","年龄:","联系电话:"};
private String []  answer= new String[]{"2015.4.30","张三","男","58","15254537894"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

listView = (ListView) findViewById(R.id.lv01);
List<Map<String,Object>> mData= new ArrayList<Map<String,Object>>();;
for(int i =0; i < answer.length; i++) {
Map<String,Object> item = new HashMap<String,Object>();
item.put("images", images[i]);
item.put("question", question[i]);
item.put("answer", answer[i]);

mData.add(item);
}
SimpleAdapter adapter = new SimpleAdapter(this,mData,R.layout.item,new String[]{"images","question","answer"},new int[]{R.id.image,R.id.question,R.id.answer});
listView.setAdapter(adapter);
}
}
没有去找图片就用android机器人代替了



这样就大功告成了~~~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐