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

案例代码

相关推荐
zhoutanooi13 分钟前
安卓bp文件编译学习
android·学习
aramae2 小时前
MySQL数据库入门指南
android·数据库·经验分享·笔记·mysql
百锦再2 小时前
选择Rust的理由:从内存管理到抛弃抽象
android·java·开发语言·后端·python·rust·go
whatever who cares2 小时前
在Java/Android中,List的属性和方法
android·java
油炸小波5 小时前
09-微服务原理篇(XXLJOB-幂等-MySQL)
android·mysql·微服务
果子没有六分钟5 小时前
setprop debug.hwui.profile visual_bars有什么作用
android
键来大师8 小时前
Android15 源码关闭Selinux
android·java·framework·rk3588
Ro Jace9 小时前
三国华容道(横刀立马)移动策略
android·java·数据库·人工智能·深度学习·神经网络·游戏
Digitally9 小时前
如何将短信从安卓手机传输到电脑
android