当前位置: 首页 > news >正文

Android开发之Spinner

Android开发之Spinner

  • 1. 概述
  • 2. Spinner
  • 3. 适配器
    • 3.1 ArrayAdapter
    • 3.2 SimpleAdapter

1. 概述

Android开发学习笔记。学习下拉框控件Spinner和适配器(数组适配器ArrayAdapter、简单适配器SimpleAdapter)的使用。

2. Spinner

下拉框控件,用于从一串列表中选择一项。

Spinner的属性spinnerMode的两个不同取值决定了下拉列表的两种展示方式:

  • 取值为dropdown时,在当前下拉框的正下方弹出列表框;
  • 取值为dialog时,在页面中部弹出列表框。

代码中,Spinner实例可调用的4个方法:

  • setPrompt,设置标题,下拉模式时不显示;
  • setAdapter,设置列表项的数据适配器;
  • setSelectioin,要在setAdapter方法后才能调用,设置当前选中哪项;
  • setOnItemSelectedListener,设置下拉框列表选择监听器,该监听器要实现接口OnItemSelectedListener。

3. 适配器

把适配器比作一组数据的加工流水线,产品就是一组待加工的数据。待加工的一组原始数据交给适配器,适配器先按数组给定的顺序排列这组原始数据,然后拿制作好的产品包装(每个列表项的布局文件)把产品包装起来,出来的便是一组完整的产品。

3.1 ArrayAdapter

数组适配器适用于每行列表只展示文本的情况。实现过程分3步:

  • 编写列表项的xml文件,内部布局只有一个TextView标签;
  • 调用ArrayAdapter的构造方法,填入待展现的字符串数组;
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this, R.layout.item_select, starArray);
  • 调用Spinner的setAdapter方法,传入数组适配器实例
sp_dialog.setAdapter(arrayAdapter);

3.2 SimpleAdapter

简单适配器,允许在列表项中同时展示文本与图片。

SimpleAdapter sa = new SimpleAdapter(this,list,R.layout.item_simple,new String[]{"icon", "name"},new int[]{R.id.iv_icon, R.id.tv_name});public SimpleAdapter(android.content.Context context,java.util.List<? extends java.util.Map<String, ?>> data,@LayoutRes int resource,String[] from,@IdRes int[] to)
/*
Constructor
Params:
context – The context where the View associated with this SimpleAdapter is runningdata – 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"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"from – A list of column names that will be added to the Map associated with each item.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.
http://www.lryc.cn/news/524460.html

相关文章:

  • 【c++继承篇】--继承之道:在C++的世界中编织血脉与传承
  • 分布式系统通信解决方案:Netty 与 Protobuf 高效应用
  • 计算机网络 (54)系统安全:防火墙与入侵检测
  • stack底层实现细节
  • 工业相机 SDK 二次开发-Halcon 插件
  • map和set的使用(一)详解
  • ARP 表、MAC 表、路由表、跨网段 ARP
  • 37.构造回文字符串问题|Marscode AI刷题
  • ssm-mybatisPlus学习笔记
  • 【算法学习笔记】35:扩展欧几里得算法求解线性同余方程
  • 线性规划:机器学习中的优化利器
  • Ubuntu开发中的问题
  • MAC 地址转换为标准大写格式
  • 使用插件SlideVerify实现滑块验证
  • 深入探索 Nginx 的高级用法:解锁 Web 服务器的强大潜能
  • (01)搭建开发环境
  • Win11桌面右键刷新选项在二级界面的修正方法
  • 配电室防静电地板通常用哪种
  • 【重庆市乡镇界】面图层shp格式arcgis数据乡镇名称和编码wgs84坐标无偏移内容测评
  • 68,[8] BUUCTF WEB [RoarCTF 2019]Simple Upload(未写完)
  • Windows电脑桌面记录日程安排的提醒软件
  • TiDB与Oracle:数据库之争,谁能更胜一筹?
  • USART_串口通讯中断案例(HAL库实现)
  • 【MySQL】存储引擎有哪些?区别是什么?
  • [OpenGL]实现屏幕空间环境光遮蔽(Screen-Space Ambient Occlusion, SSAO)
  • linux-NFS网络共享存储服务配置
  • w-form-select.vue(自定义下拉框组件)
  • ovs实现lb负载均衡
  • 机器学习-核函数(Kernel Function)
  • 计算最接近的数