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

案例代码

相关推荐
AirDroid_cn2 小时前
Realme手机怎样相互远程控制?Realme可以被其他手机远程控制吗?
android·智能手机·远程工作·远程控制·远程控制手机
Yang-Never5 小时前
Kotlin -> 普通Lambda vs 挂起Lambda
android·开发语言·kotlin·android studio
来来走走5 小时前
Flutter开发 MaterrialApp基本属性介绍
android·flutter
智江鹏5 小时前
Android 之 图片加载(Fresco/Picasso/Glide)
android
2501_915921435 小时前
移动端 WebView 视频无法播放怎么办 媒体控件错误排查与修复指南
android·ios·小程序·https·uni-app·iphone·webview
安卓开发者10 小时前
Android JUnit 测试框架详解:从基础到高级实践
android·junit·sqlserver
hcgeng10 小时前
如何在Android中创建自定义键盘布局
android·keyboard
Jomurphys10 小时前
Android 优化 - 日志 Log
android
狂浪天涯11 小时前
Android 16 显示系统 | 从View 到屏幕系列 - 7 | SurfaceFling Commit
android
_祝你今天愉快12 小时前
HashMap 底层原理 (JDK 1.8 源码分析)
android·java·后端