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. git和repo入门

    版本控制 版本控制是什么已不用在说了,就是记录我们对文件.目录或工程等的修改历史,方便查看更改历史,备份以便恢复以前的版本,多人协作... 一.原始版本控制 最原始的版本控制是纯手工的版本控制:修改文 ...

  2. bzoj 3295 [Cqoi2011]动态逆序对(cdq分治,BIT)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3295 [题意] n个元素依次删除m个元素,求删除元素之前序列有多少个逆序对. [思路] ...

  3. Android字符串相关类 - CharSequence

    Class Overview CharSequence定义为public interface.该接口用于表示一个有序字符的集合,并在其中定义里了处理字符的方法. 已知的常用间接子类有String, S ...

  4. 50道经典的JAVA编程题 (6-10)

    50道经典的JAVA编程题 (6-10),今晚做了10道了,累死了...感觉难度不是很大,就是不知道是不是最好的实现方法啊!希望大神们能给指点哈... [程序6]GCDAndLCM.java 题目:输 ...

  5. [转]Hadoop YARN任务提交流程

    Yarn是随着hadoop发展而催生的新框架,全称是Yet Another Resource Negotiator,可以翻译为“另一个资源管理器”.yarn取代了以前hadoop中jobtracker ...

  6. Android Studio 模拟器启动问题——黑屏 死机 解决方法

    今天用了下Android Studio,出现了一些问题,现在将启动过程中遇到的问题和解决方案列出来,方便大家参考. 安装过程不多说,网上一搜一大把. 那直接说问题吧: 1. 无法启动,报错:Faile ...

  7. 教程-Supports判断接口(Instance)是否支持

    function TCommandEnabledController.GetCommandVisible(const ACommandName: string): Boolean; var I: In ...

  8. 关于Windows文件名和路径名的那些事

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:关于Windows文件名和路径名的那些事.

  9. Css基础-派生选择器

    如果要修改li strong 里面文字的颜色可以这样写样式 派生选择器: li strong { color:red; } 效果:

  10. linux mail命令用法

    在Linux系统下mail命令的测试 1. 最简单的一个例子: mail -s test admin@aispider.com 这条命令的结果是发一封标题为test的空信给后面的邮箱,如果你有mta并 ...