Android中简单实现Spinner的数据绑定

Android中简单实现Spinner的数据绑定

然后声明对象实例并加入到arraylist里面,并设置spinner的适配器

Spinner Sp = (Spinner).............//

List<CItem > lst = new ArrayList<CItem>();

CItem ct = new CItem ("1","测试");

lst.Add(ct);;

ArrayAdapter<CItem > Adapter = new ArrayAdapter<CItem>(context,

android.R.layout.simple_spinner_item, lst);

Sp.SetAdapter(Adapter);

取值:

如果取TEXT值则可以直接取:Sp.getSelectedItem.ToString()或者:((CItem)Sp.getSelectedItem). GetValue() ;

如果取Value值则可以这样取:((CItem)Sp.getSelectedItem).GetID();

java 复制代码
    @Override
    public String toString() {
        // 为什么要重写toString()呢?
        // 因为适配器在显示数据的时候,
        // 如果传入适配器的对象不是字符串的情况下,
        // 直接就使用对象.toString()
        return milktype_name;
    }
相关推荐
牛哇网络工作室6 小时前
UnityHDRP写实数字人全流程基础5—语音输入和语音识别
android·unity·c#·游戏引擎·aigc·语音识别·xcode
一笑的小酒馆7 小时前
Androidiot蓝牙配网简单封装
android
愚公搬代码10 小时前
【愚公系列】《Android应用案例开发大全》016-LBS类应用掌上杭州(辅助工具类的开发)
android·前端
g105655913910 小时前
LAMP博客平台Wordpress实战
android·mysql·nginx·php
hunterandroid11 小时前
[Android 从零到一] Compose LazyColumn 性能优化:key、稳定性与重组治理
android·前端
pengyu12 小时前
【Kotlin 协程修仙录 · 元婴境 · 中阶】 | 操作符真解:Flow 中间操作符与数据流变幻术
android·kotlin
pengyu13 小时前
【Kotlin 协程修仙录 · 元婴境 · 初阶】 | 冷流初啼:Flow 的基础与响应式编程的入门
android·kotlin
wawo0013 小时前
Kuikly实践-从Android compose迁移到kuikly compose
android
Carson带你学Android15 小时前
Android 版 MCP 正式登场:AppFunctions
android·ai编程·jetbrains
古法安卓18 小时前
Android-Direct Boot 阶段与 getFilesDir() 路径变更问题深度分析
android·java·android studio