spinner中Adapter的两种使用方式

本文介绍了在Android中如何使用ArrayAdapter和自定义Adapter实现Spinner下拉列表的功能。通过步骤演示了ArrayAdapter的基本用法,以及如何创建自定义Adapter以添加更多自定义功能,包括设置点击事件和显示效果。
摘要由CSDN通过智能技术生成

Adapter的使用

Android 中adapter的两种使用方式

ArrayAdapter和自定义adapter

实现的目标

  1. **Android studio 实现下拉列表
    如图:在这里插入图片描述
    在这里插入图片描述点击图一小三角后有如图二的下拉列表

方法一:使用ArrayAdapter

ArrayAdapter是Android studio中自带的基础适配器,使用还是比较方便的,我们来看看怎么使用。
1:声明适配器
private ArrayAdapter arrayAdapter;
2:创建适配器
arrayAdapter=new ArrayAdapter(this,R.layout.support_simple_dropdown_item,数据对象);
3:使用适配器
spinner控件.setArrayAdapter(arrayAdapter);

下面开始实施步骤

****1:先创建xml文件用来展示视图

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".control.ac_spinner"
    android:orientation="vertical">

    <Spinner
        android:id="@+id/sp_test"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <TextView
        android:id="@+id/tv_sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="spinner控件adapter的使用示例"/>
</LinearLayout>
**2:创建activity文件,引用后有错误按alt+enter键可以解决**
package com.example.meimu.control;

import android.os.Bundle;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.meimu.R;
import com.example.meimu.adapter.adapter_spinner;
import com.example.meimu.entity.group;

import java.util.ArrayList;
import java.util.List;

public class ac_spinner extends AppCompatActivity
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值