Android——Adapter

ArrayAdapter

layout/item_select.xml

xml 复制代码
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:gravity="center"
    android:textColor="#0000ff"
    android:textSize="17sp"
    android:text="火星"/>
xml 复制代码
    <Spinner
        android:id="@+id/sp_dropdown"
        android:layout_width="match_parent"
        android:layout_height="19dp"
        android:spinnerMode="dropdown"/>
java 复制代码
	private Spinner sp_dropdown;
    private static final String[] starArray = {"金星", "水星", "木星","地球", "木星", "土星"};
java 复制代码
 	ArrayAdapter<String> starArrayAdapter = new ArrayAdapter<>(this, R.layout.item_select, starArray);
    sp_dropdown.setAdapter(starArrayAdapter);

SimpleAdapter

java 复制代码
    private static final int[] iconArray = {
            R.drawable.shuixing, R.drawable.tuxing,
            R.drawable.muxing, R.drawable.diqiu,
            R.drawable.huoxing, R.drawable.jinxing,
    };
    private static final String[] starArray = {"金星", "水星", "木星", "地球", "木星", "土星"};
java 复制代码
        List<Map<String, Object>> list = new ArrayList<>();

        for (int i = 0; i < iconArray.length; i++) {
            Map<String, Object> item = new HashMap<>();
            item.put("icon", iconArray[i]);
            item.put("name", starArray[i]);

            list.add(item);
        }
        SimpleAdapter starSimpleAdapter = new SimpleAdapter(this, list,
                R.layout.item_simple,
                new String[]{"icon", "name"},
                new int[]{R.id.iv_icon, R.id.tv_name});

        sp_dropdown.setAdapter(starSimpleAdapter);
        sp_dropdown.setSelection(0);
        sp_dropdown.setOnItemSelectedListener(this);

案例代码

相关推荐
一笑的小酒馆15 小时前
Android性能优化之截屏时黑屏卡顿问题
android
懒人村杂货铺18 小时前
Android BLE 扫描完整实战
android
TeleostNaCl20 小时前
如何安装 Google 通用的驱动以便使用 ADB 和 Fastboot 调试(Bootloader)设备
android·经验分享·adb·android studio·android-studio·android runtime
fatiaozhang952721 小时前
中国移动浪潮云电脑CD1000-系统全分区备份包-可瑞芯微工具刷机-可救砖
android·网络·电脑·电视盒子·刷机固件·机顶盒刷机
2501_915918411 天前
iOS 开发全流程实战 基于 uni-app 的 iOS 应用开发、打包、测试与上架流程详解
android·ios·小程序·https·uni-app·iphone·webview
lichong9511 天前
【混合开发】vue+Android、iPhone、鸿蒙、win、macOS、Linux之dist打包发布在Android工程asserts里
android·vue.js·iphone
Android出海1 天前
Android 15重磅升级:16KB内存页机制详解与适配指南
android·人工智能·新媒体运营·产品运营·内容运营
一只修仙的猿1 天前
毕业三年后,我离职了
android·面试
编程乐学1 天前
安卓非原创--基于Android Studio 实现的新闻App
android·ide·android studio·移动端开发·安卓大作业·新闻app
雅雅姐1 天前
Android14 init.rc中on boot阶段操作4
android