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. HDU 5319 Painter

    题意:红色从左上向右下涂,蓝色从右上向左下涂,既涂红色又涂蓝色就变成绿色,问最少涂几下能变成给的图. 解法:模拟一下就好了,注意细节. 代码: #include<stdio.h> #inc ...

  2. HDU 1078 FatMouse and Cheese 记忆化搜索DP

    直接爆搜肯定超时,除非你加了某种凡人不能想出来的剪枝...555 因为老鼠的路径上的点满足是递增的,所以满足一定的拓补关系,可以利用动态规划求解 但是复杂的拓补关系无法简单的用循环实现,所以直接采取记 ...

  3. 9.19AD和DA操作

    下载芯片说明书的网站:http://www.21ic.com/ D/A digital是数字信号,analog是模拟信号,单片机属于数字芯片,内部只有0和1,这两种信息无法表示一个模拟量,如果是一个8 ...

  4. NOR型flash与NAND型flash的区别

    1) 闪存芯片读写的基本单位不同  应用程序对NOR芯片操作以“字”为基本单位.为了方便对大容量NOR闪存的管理,通常将NOR闪存分成大小为128KB或者64KB的逻辑块,有时候块内还分成扇区.读写时 ...

  5. RequestHander类介绍

    RequestHander是一个抽象类,是一个线程.它封装了一个Socket.代码不难: package org.simpleHTTPServer; import java.io.IOExceptio ...

  6. HW5.35

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  7. HDU-4648 Magic Pen 6 简单题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4648 求遍前缀和,然后扫描标记下就可以了... //STATUS:C++_AC_453MS_1792K ...

  8. POJ-3714 Raid 平面最近点对

    题目链接:http://poj.org/problem?id=3714 分治算法修改该为两个点集的情况就可以了,加一个标记... //STATUS:C++_AC_2094MS_4880KB #incl ...

  9. 安装禅道项目管理软件ZenTaoPMS

    服务器Ubuntu 13.04 且安装了上一篇随笔中的 AMP本文略去安装AMP过程.版本号满足要求(php>5.2 and mysql.2) 1.官网http://www.zentao.net ...

  10. 在iOS应用程序中打开设备设置界面及其中某指定的选项界面

    摘自:http://stackoverflow.com/questions/8246070/ios-launching-settings-restrictions-url-scheme [[UIApp ...