Android学习笔记之SimpleAdapter
2014-02-25 21:07
483 查看
这是一个简单的适配器,可以将静态数据映射到XML文件中定义好的视图。
一般用于ListActivity中的ListView
在ListView的布局当中作为
setListAdapter()的参数来填充List当中的内容
public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
构造函数
参数
context 关联SimpleAdapter运行着的视图的上下文。
data
一个Map的列表(List对象 里面是map,类似于
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
)。在列表中的每个条目对应列表中的一行,应该包含所有在from中指定的条目
resource 一个定义列表项目的视图布局的资源唯一标识。布局文件将至少应包含哪些在to中定义了的名称。
from 一个将被添加到Map上关联每一个项目的列名称的列表
to 应该在参数from显示列的视图。这些应该全是TextView。在列表中最初的N视图是从参数from中最初的N列获取的值。
main.xml
other.xml
android:spinnerMode="dialog"没有这个的话样式会发生较大的变化
摘自 落日小屋
一般用于ListActivity中的ListView
在ListView的布局当中作为
setListAdapter()的参数来填充List当中的内容
public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
构造函数
参数
context 关联SimpleAdapter运行着的视图的上下文。
data
一个Map的列表(List对象 里面是map,类似于
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
)。在列表中的每个条目对应列表中的一行,应该包含所有在from中指定的条目
resource 一个定义列表项目的视图布局的资源唯一标识。布局文件将至少应包含哪些在to中定义了的名称。
from 一个将被添加到Map上关联每一个项目的列名称的列表
to 应该在参数from显示列的视图。这些应该全是TextView。在列表中最初的N视图是从参数from中最初的N列获取的值。
package com.example.imageview1activity; 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.SimpleAdapter; import android.widget.Spinner; public class ImageView1Activity extends Activity { /** Called when the activity is first created. */ private Spinner spinner1 = null; private List<Map<String,Object>> list = null; private SimpleAdapter adapter = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); list = new ArrayList<Map<String,Object>>(); Map<String,Object> hashmap = new HashMap<String,Object>(); hashmap.put("itemsrc",R.drawable.ic_launcher); hashmap.put("itemname","信息学院"); list.add(hashmap); HashMap<String,Object> hashmap1 = new HashMap<String,Object>(); hashmap1.put("itemsrc",R.drawable.ic_launcher); hashmap1.put("itemname","信电学院"); list.add(hashmap1); HashMap<String,Object> hashmap2 = new HashMap<String,Object>(); hashmap2.put("itemsrc",R.drawable.ic_launcher); hashmap2.put("itemname","经管学院"); list.add(hashmap2); spinner1 = (Spinner)findViewById(R.id.spinner1); spinner1.setPrompt("你要以后在哪工作?"); adapter = new SimpleAdapter(ImageView1Activity.this,list,R.layout.other,new String[]{"itemsrc","itemname"},new int[]{R.id.text1,R.id.text2}); spinner1.setAdapter(adapter); } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Spinner"/> <Spinner android:id="@+id/spinner1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:spinnerMode="dialog"/> </LinearLayout>
other.xml
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TableRow> <ImageView android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/text2" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </TableRow> </TableLayout>
android:spinnerMode="dialog"没有这个的话样式会发生较大的变化
摘自 落日小屋
相关文章推荐
- android学习笔记之ListView 和Adapter适配器BaseAdapter,simpleAdapter、adrrayAdapter
- Android学习笔记之SimpleAdapter
- Android学习笔记——ExpandableListActivity与SimpleExpandableListAdapter
- Android学习笔记(八):SimpleAdapter的学习
- Android学习笔记(十八)——ExpandableListActivity与SimpleExpandableListAdapter
- Android学习笔记之SimpleAdapter
- Android学习笔记之 SimpleAdapter 中添加按钮响应事件,getView的重写
- Android中GridView的使用——使用自带的SimpleAdapter(简单适配器)
- Android SimpleAdapter应用介绍,代码+解释 ListView使用+滚动监听用scoll
- android ListView利用SimpleAdapter显示特定布局并且异步加载网络图片
- Android SimpleAdapter简单实例和SimpleAdapter参数说明
- 为SimpleCursorAdapter增加CheckBox支持
- 简单使用SimpleCursorAdapter
- SimpleAdapter适配的ListView(不常用)
- ArrayAdapter,SimpleAdapter的基本使用
- 自定义SimpleAdapter
- SimpleAdapter 和 ListView
- SimpleExpandableListAdapter
- simpleAdapter 简单实用方法
- [转]Android适配器之ArrayAdapter、SimpleAdapter和BaseAdapter的简单用法与有用代码片段