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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"/> </RelativeLayout>

适配器显示布局文件:

<?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/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/> </LinearLayout>

源代码:

package com.example.day04_simpleadapter;

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.view.View;
import android.widget.SimpleAdapter;
import android.widget.Spinner; public class MainActivity extends Activity { private Spinner spinner;
private String[] city;
private int[] pic; /* SimpleAdapter:
arraydapter 只能显示单个文本(太单一)
SimpleAdapter 可以放置复杂样式的条目
SimpleAdapter <List<Map<String,Object>>> adapter = new SimpleAdapter<>(
Context context,// 上下文对象
List<? extends Map<String, ?>> data,// 表示的是数据 ,适配控件的的每一条数据用list装, 数据用map去表示 map中的key与from参数对应
int resource, // 一条数据(一个条目)显示的布局
String[] from,// 与map中的多个key对应
int[] to,// 是需要显示数据的控件的id,该id的顺序必须与from中的key对应(即key获取的值要放到id对应的控件上)
);
*/ @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner) findViewById(R.id.spinner);
city = new String[] {"北京","上海","杭州","深圳","广州"};
pic = new int[] {R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher}; SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, getdata(), R.layout.layout01, new String[]{"city","pic"},new int[]{R.id.text,R.id.image});
spinner.setAdapter(adapter);
} private List<? extends Map<String, ?>> getdata() {
// TODO Auto-generated method stub
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
Map<String,Object>map = null;
for (int i = 0; i < city.length; i++) {
map = new HashMap<String, Object>();
map.put("city",city[i]);
map.put("pic", pic[i]);
list.add(map);
}
return list;
} }

Android_Spinner_SimpleAdapter的更多相关文章

随机推荐

  1. CF GYM 100703A Tea-drinking

    题意:龙要制作n个茶,每个茶的配方是一个字符串,两个字符串之间有一个差值,这个差值为两个字符串每个对应字母之间差的绝对值的最大值,求制作所有茶时获得的所有差值中的最大值. 解法:克鲁斯卡尔.将茶的配方 ...

  2. json解析之jackson ObjectMapper

    Json解析常用的有fastjson和jackson,性能上网上有不少的对比,说是fastjson比较好,今天先整理一下jackson的东西,后面再发一个fastjson的. jackson是spri ...

  3. js 中&& 与 ||

    /*** 几乎所有语言中||和&&都遵循“短路”原理,* 如&&中第一个表达式为假就不会去处理第二个表达式,而||正好相反.* js也遵循上述原则.* 当||时,找到为 ...

  4. linux操作系统cp命令

  5. python 网络编程(五)---DNS域名系统

    1.域名系统定义 DNS计算机域名系统由域名服务器和域名解析器组成.通常输入的是网址就是一个域名. 2.域名查询 查询方式包括: 1)正向查询:由域名查找对应的IP(如:www.baidu.com—& ...

  6. Android 依赖注入 ButterKnife 基本使用

    ButterKnife 是一个快速 Android View 注入框架,开发者是Jake Wharton,简单的来说,ButterKnife 是用注解的方式替代findViewById和setXXXL ...

  7. HDU-4671 Backup Plan 构造解

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4671 假设是3 m,首先按照第一列按照1 2 3 1 2 3 1...排下去,然后个数就是一个 (m/ ...

  8. hdu4436-str2int(后缀数组 or 后缀自动机)

    题意:给你一堆字符串,仅包含数字'0'到'9'. 例如 101 123 有一个字符串集合S包含输入的N个字符串,和他们的全部字串. 操作字符串很无聊,你决定把它们转化成数字. 你可以把一个字符串转换成 ...

  9. Delphi外挂开发网站

    http://cheatengine.org/http://wenku.baidu.com/view/2d5de818964bcf84b9d57b15.html   [delphi外G]http:// ...

  10. 教程-Delphi各版本与工具下载地址

    1.Delphi 7.0 下载地址:http://www.skycn.com/soft/2121.html 注册码:(正在用的没有问题)V8S3-KM82KQ-XN8JQK-EPS33EA-GZK汉化 ...