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);

案例代码

相关推荐
SHEN_ZIYUAN7 小时前
深度解析:从 AnimationHandler 原理看 Android 动画内存泄漏
android
冬奇Lab7 小时前
稳定性性能系列之十六——车机特定场景:黑卡死问题分析与排查实战
android·性能优化
座山雕~8 小时前
Springboot
android·spring boot·后端
香气袭人知骤暖9 小时前
SQL慢查询常见优化步骤
android·数据库·sql
丨康有为丨9 小时前
Android滑动冲突详解(场景+解决)
android
千里马学框架10 小时前
疑难ANR面试题:crash导致ANR深入剖析
android·智能手机·framework·perfetto·性能·anr·小米汽车
石像鬼₧魂石13 小时前
安卓 WiFi 钓鱼渗透测试全流程教程(详细版)
android
YIN_尹14 小时前
【MySQL】表的约束(上)
android·数据库·mysql
_李小白14 小时前
【Android 美颜相机】第二天:Android-GPUImage Sample模块源码解析
android·数码相机
2501_9159090614 小时前
Charles 抓不到包怎么办?iOS 调试过程中如何判断请求路径
android·ios·小程序·https·uni-app·iphone·webview