ArrayAdapter主要用于每行列表只显示文本的情况,而SimpleAdapter则还可以给列表加上图标,允许在列表项中展示多个控件。

下面以下拉框Sipnner为例,说明SimpleAdapter的用法。

layout文件夹里的activity_spinner.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:padding="15dp"android:orientation="vertical"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="请选择:"android:textSize="26sp"android:gravity="center"android:paddingBottom="10dp"/><Spinnerandroid:id="@+id/sp_icon"android:layout_width="match_parent"android:layout_height="wrap_content"android:spinnerMode="dialog" />"</LinearLayout>

对应的SpinnerActivity类

package com.example.myapplication;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.Toast;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;public class SpinnerActivity extends AppCompatActivity {//iconArray是行星的图标数组,starArray是行星的名称数组private int[] iconArray = {R.drawable.shuixing, R.drawable.jinxing, R.drawable.diqiu,R.drawable.huoxing, R.drawable.muxing, R.drawable.tuxing};private String[] starArray = {"水星", "金星", "地球", "火星", "木星", "土星"};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_spinner);//声明一个映像对象的队列,用于保存行星的图标与名称配对信息List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();for (int i = 0; i < iconArray.length; i++) {Map<String, Object> item = new HashMap<String, Object>();item.put("icon", iconArray[i]);item.put("name", starArray[i]);list.add(item);}//声明一个下拉列表的简单适配器,其中制定了图标与文本两组数据SimpleAdapter starAdapter = new SimpleAdapter(this, list,R.layout.item_select, new String[] { "icon", "name" },new int[] {R.id.iv_icon, R.id.tv_name});//设置简单适配器的布局样式starAdapter.setDropDownViewResource(R.layout.item_simple);//从布局文件中获取名叫sp_icon的下拉框Spinner sp = (Spinner) findViewById(R.id.sp_icon);//设置下拉框的标题sp.setPrompt("请选择行星");//设置下拉框的简单适配器sp.setAdapter(starAdapter);//设置下拉框默认显示的第一项sp.setSelection(0);//给下拉框设置选择监听器,一旦用户选中某一项,就触发监听器的onItemSelected方法sp.setOnItemSelectedListener(new MySelectedListener());}class MySelectedListener implements AdapterView.OnItemSelectedListener {public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {Toast.makeText(SpinnerActivity.this, "您选择的是"+starArray[arg2], Toast.LENGTH_SHORT).show();}public void onNothingSelected(AdapterView<?> arg0) {}}
}

layout文件夹里的item_select.xml和item_simple.xml。分别用于设置选中item的样式和下拉框的样式。

<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tv_name"android:layout_width="match_parent"android:layout_height="wrap_content"android:singleLine="true"android:gravity="center"android:textSize="17sp"android:textColor="#0000ff" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal" ><ImageViewandroid:id="@+id/iv_icon"android:layout_width="0dp"android:layout_height="50dp"android:layout_weight="1"android:gravity="center" /><TextViewandroid:id="@+id/tv_name"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="3"android:gravity="center"android:textSize="17sp"android:textColor="#ff0000" /></LinearLayout>

在此分析一下SimpleAdapter的源码

public class SimpleAdapter extends BaseAdapter implements Filterable, ThemedSpinnerAdapter{private final LayoutInflater mInflater;private int[] mTo;private String[] mFrom;private ViewBinder mViewBinder;@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)private List<? extends Map<String, ?>> mData;private int mResource;private int mDropDownResource;/** Layout inflater used for {@link #getDropDownView(int, View, ViewGroup)}. */private LayoutInflater mDropDownInflater;private SimpleFilter mFilter;private ArrayList<Map<String, ?>> mUnfilteredData;public SimpleAdapter(Context context, List<? extends Map<String, ?>> data,@LayoutRes int resource, String[] from, @IdRes int[] to) {mData = data;mResource = mDropDownResource = resource;mFrom = from;mTo = to;mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);}.............public void setDropDownViewResource(int resource) {mDropDownResource = resource;}.............
}

可以看出,SimpleAdapter类继承了BaseAdapter抽象类,实现了Filterable和ThemedSpinnerAdapter接口。而BaseAdapter类又实现了ListAdapter和SpinnerAdapter接口。

关于该类的构造方法,一共有五个参数。

/*** Constructor** @param context The context where the View associated with this SimpleAdapter is running* @param data A List of Maps. Each entry in the List corresponds to one row in the list. The*        Maps contain the data for each row, and should include all the entries specified in*        "from"* @param resource Resource identifier of a view layout that defines the views for this list*        item. The layout file should include at least those named views defined in "to"* @param from A list of column names that will be added to the Map associated with each*        item.* @param to The views that should display column in the "from" parameter. These should all be*        TextViews. The first N views in this list are given the values of the first N columns*        in the from parameter.*/

context:上下文;

data:需要展示的数据源,是map组成的list集合,每个Map对应ListView中的一行,每一个Map(键——值对)中的键名必须包含在from中所指定的键;

resource:展示的布局文件,可以自己在layout设置xml文件去设置需要展示 的样式;

from:Map中的键名;

to:绑定数据视图的ID,与from成对应的关系。

第二个参数是关于Map集合的List集合。定义为List<Map<String,Object>>。

new了一个starAdapter对象之后,便可以进行简单适配器的布局样式的设置。以及对下拉框的各种属性进行设置。

注意:setSelection方法要在setAdapter方法后调用。

运行效果如下:

资料来源:Android Studio开发实战

关于SimpleAdapter的用法相关推荐

  1. SimpleAdapter的用法

    学习listView的时候,按照例子设定item的布局为系统提供的simple_list_item_single_choice.xml@frameworks/base/core/res/res/lay ...

  2. android ListView适配器之SimpleAdapter的用法

    以前写过ListView的适配器中最简单的ArrayAdapter这个适配器,当listView中只需要显示一个数据时,使用ArrayAdapter适配器很方便,但是如果要向listview的每一行显 ...

  3. Android Listview SimpleAdapter的使用完整示例(实现用户列表)

    上一篇文章我们讲了Android Listview ArrayAdapter示例_左眼看成爱的博客-CSDN博客 这篇文章我们来讲一下 Android Listview基于SimpleAdapter的 ...

  4. Android新手入门2016(10)--GridView

    本文来自肥宝传说之路,引用必须注明出处! GridView跟ListView一样是多控件布局.实现九宫图是最方便的. 还是先看看图,没图说个鸡鸡是不是 如上图,是一种应用方式,在每个格子里面,放入应用 ...

  5. Java面试题大全(Android版)

    疯狂Java面试题大全(Android版) Java核心技术部分 Java核心技术部分的面试题,可能覆盖Java基本语法.面向对象(包括类定义.方法.构造器.递归.继承.抽象类.接口.枚举以及fina ...

  6. Android基础与手机历史

    智能的机操作系统经过多盘厮杀,到现在形成了三足鼎立之势:Android.iOS.Windows phone 8三者一统天下.而其他的诸如Symbian S60, UIQ, Windows Mobile ...

  7. 疯狂Java面试题大全(Android版)

    疯狂Java面试题大全(Android版) 本大全每个月会定期更新,索取网址:http://www.fkjava.org Java核心技术部分 Java核心技术部分的面试题,可能覆盖Java基本语法. ...

  8. 疯狂Java和Android面试题大全(Android版)

    疯狂Java和Android面试题大全(Android版) 本大全每个月会定期更新,索取网址:http://www.fkjava.org Java核心技术部分 Java核心技术部分的面试题,可能覆盖J ...

  9. android的fragment添加列表,Android之listfragment的使用例子

    1.fragment简介 我对fragment的理解是基于activity的,对于大多数的基本开始发时,我们最先遇到的就是用activity来开发. 简单的例子,新建一个最基本的Android空白界面 ...

最新文章

  1. linux进程间通信:无名管道 pipe
  2. HDFS Federation与HDFS High Availability详解
  3. java应用cpu使用率过高问题排查
  4. linux c之判断字符串是不是以另一字符串开始或者结尾
  5. 会议交流 | IJCKG 2021:Keynotes released!欢迎注册参会
  6. 图(关系网络)数据分析及阿里应用
  7. Android SQLite (二) 基本用法
  8. php 把java list对象转成数组,java_JSON的String字符串与Java的List列表对象的相互转换,在前端: 1.如果json是List对象 - phpStudy...
  9. 20200203_selenium爬取百度新闻
  10. Android头部悬浮ListView第二种实现方式
  11. 帆软怎样从mysql里导入数据源_怎样创建数据库?4种流行数据库的创建方法介绍...
  12. 2021年MathorCup数学建模A题自动驾驶中的车辆调头问题全过程解题论文及程序
  13. Python爬虫实例项目大全
  14. 一条让人不安的坐地龙
  15. 三维扫描仪[8]——如何设计一台云台式扫描仪(机械结构)
  16. 简洁明了的个人求职简历如何写?
  17. Drupal7学习笔记之Theme感觉非常好转来共享啊!
  18. openGL中Phong 着色
  19. 【Vertica系列】一、安装建库
  20. 《TiDB 6.x in Action》发布,凝聚社区集体智慧的 6.x 实践汇总!

热门文章

  1. 那些科技巨头们的第一次
  2. 让ADOBE系列软件恢复英文版
  3. 【设计模式】行为型01策略模式(strategy patten)
  4. 阿里妈妈API接口 按关键字或网址搜索商品
  5. 最全的机器学习amp;深度学习入门视频课程集
  6. 将服务器端口映射到本地端口
  7. 基于威胁分析的无人机数据安全传输系统论文怎么写
  8. Git svn 混用实践
  9. matlab安装RoboticsToolBox rvctools工具箱
  10. Visual Attribute Transfer through Deep Image Analogy论文阅读笔记