Android : Spinner(列表選項框) + BaseAdapter -簡單應用

???容器與適配器:?????????????http://t.csdnimg.cn/ZfAJ7

示例圖:

實體類 Demo.java

package com.example.mygridviewadapter.entity;public class Demo {private String  text;private  int img;public Demo(String text, int img) {this.text = text;this.img = img;}public String getText() {return text;}public void setText(String text) {this.text = text;}public int getImg() {return img;}public void setImg(int img) {this.img = img;}
}

適配器 寫個類繼承BaseAdapter?

package com.example.myspinnerbaseadapter;import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;import com.example.myspinnerbaseadapter.entity.Data;import java.util.List;public class DataAdapter extends BaseAdapter {private List<Data> dataList;private Context context;static class ViewHoler{ImageView imageView;TextView textView;}//BaseAdapter最基本的幾個方法:// 1. getCount 填充的數據集數// 2.getItem 數據集中指定索引對應的數據項// 3. getItemId 指定行所對應的ID// 4. getView 每個Item所顯示的類容public DataAdapter(List<Data> dataList, Context context) {this.dataList = dataList;this.context = context;}@Overridepublic int getCount() {return dataList.size();}@Overridepublic Object getItem(int position) {return dataList.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHoler holer = null;if(holer == null){holer = new ViewHoler();//獲取 自定義view布局 獲取相關屬性convertView = LayoutInflater.from(context).inflate(R.layout.spinner_view,parent,false);holer.imageView = convertView.findViewById(R.id.bntImg);holer.textView = convertView.findViewById(R.id.btnTitle);convertView.setTag(holer);}else {holer = (ViewHoler)convertView.getTag();}//設置值holer.imageView.setBackgroundResource(dataList.get(position).getImg());holer.textView.setText(dataList.get(position).getTitile());return convertView;}
}

MainActivity.java

package com.example.myspinnerbaseadapter;import androidx.appcompat.app.AppCompatActivity;import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;import com.example.myspinnerbaseadapter.entity.Data;import java.util.ArrayList;
import java.util.List;public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{//組件private Spinner spinnerOne,spinner;private List<Data> dataList =null;private Context context;private DataAdapter dataAdapter =null;//圖片private int[] arr = {R.mipmap.a,R.mipmap.b,R.mipmap.c,R.mipmap.d,R.mipmap.e,R.mipmap.f,};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//上下文context = this;spinnerOne = findViewById(R.id.tbnSpiOne);spinner = findViewById(R.id.tbnSpi);dataList = new ArrayList<>();//循環傳入數據for (int i = 0 ; i < arr.length;i++){dataList.add(new Data("樣式"+i,arr[i]));}dataAdapter = new DataAdapter(dataList,context);// 往容器中設置適配器spinner.setAdapter(dataAdapter);//事件spinnerOne.setOnItemSelectedListener(this);spinner.setOnItemSelectedListener(this);}@Overridepublic void onItemSelected(AdapterView<?> parent, View view, int position, long id) {if(parent.getId() == R.id.tbnSpiOne){Toast.makeText(this,"您選擇了"+parent.getItemAtPosition(position),Toast.LENGTH_SHORT).show();} else if (parent.getId() == R.id.tbnSpi) {TextView textView = findViewById(R.id.btnTitle);Toast.makeText(this,"您選擇了"+textView.getText().toString(),Toast.LENGTH_SHORT).show();}}@Overridepublic void onNothingSelected(AdapterView<?> parent) {}
}

activity_main.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"android:orientation="vertical"tools:context=".MainActivity"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="請選擇頭像:"android:textSize="16sp"/><!-- 下拉列表entries :數據源android:dropDownHorizontalOffset:設置列表框的水平偏移距離android:dropDownVerticalOffset:設置列表框的水平豎直距離android:dropDownSelector:列表框被選中時的背景android:dropDownWidth:設置下拉列表框的寬度android:gravity:設置里面組件的對其方式android:popupBackground:設置列表框的背景android:prompt:設置對話框模式的列表框的提示信息(標題),只能夠引用string.xml中的資源 id,而不能直接寫字符串android:spinnerMode:列表框的模式,有兩個可選值: dialog:對話框風格的窗口dropdown: 下拉菜單風格的窗口(默認) 可選屬性:android:entries:使用數組資源設置下拉列表框的列表項目
--><Spinnerandroid:id="@+id/tbnSpiOne"android:prompt="@string/title"android:entries="@array/data"android:layout_width="wrap_content"android:layout_height="wrap_content"android:dropDownWidth="100dp"android:spinnerMode="dialog"/><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="請選擇頭像:"android:textColor="#ff00ff"android:textSize="16sp"/>--><Spinnerandroid:id="@+id/tbnSpi"android:layout_width="wrap_content"android:layout_height="wrap_content"/>
</LinearLayout>

spinner_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><!-- 圖片--><ImageViewandroid:id="@+id/bntImg"android:layout_width="64dp"android:layout_height="64dp"/><!--文字 --><TextViewandroid:id="@+id/btnTitle"android:layout_width="match_parent"android:layout_height="64dp"android:text="樣式1"android:textSize="24sp"android:gravity="center_vertical"/></LinearLayout>

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/163805.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/163805.shtml
英文地址,請注明出處:http://en.pswp.cn/news/163805.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

虛擬機解決Linux中Uos和Deepin登錄密碼忘記的問題 標題Linux Uos Deepin

Uos是切換網絡模式解決的(之前有綁定過用戶) 因為之前用的是橋接模式登錄的時候一直無法聯網,改為Nat模式后可以和電腦共用一個網絡ip,可以重置密碼了,以此解決 ps: 特別說明rw single init/bin/bash 方法和systemd.debug-shell1方法已經失效,不要再做無謂的嘗試了Deepin23社區…

Vue + Element UI 實現復制當前行數據功能(復制到新增頁面組件值不能更新等問題解決)

1、需求 使用Vue Element UI 實現在列表的操作欄新增一個復制按鈕&#xff0c;復制當前行的數據可以打開新增彈窗后亦可以跳轉到新增頁面&#xff0c;本文實現為跳轉到新增頁面。 2、實現 1&#xff09;列表頁 index.vue <el-table> <!-- 其他列 --> <el-t…

JOSEF 漏電繼電器 LLJ-100FG φ45 50-500mA 卡軌安裝

系列型號&#xff1a; LLJ-10F(S)漏電繼電器LLJ-15F(S)漏電繼電器LLJ-16F(S)漏電繼電器 LLJ-25F(S)漏電繼電器LLJ-30F(S)漏電繼電器LLJ-32F(S)漏電繼電器 LLJ-60F(S)漏電繼電器LLJ-63F(S)漏電繼電器LLJ-80F(S)漏電繼電器 LLJ-100F(S)漏電繼電器LLJ-120F(S)漏電繼電器LLJ-125F(S…

推薦一個簡單的在線壓縮PNG和JPG圖片大小的網址

問題描述&#xff1a;推薦一個簡單的在線壓縮PNG和JPG圖片大小的網址 解決&#xff1a; https://www.iloveimg.com/zh-cn/compress-image/compress-png

將對象轉成URL參數

背景 有的時候前端跳轉到其他平臺的頁面需要攜帶額外的參數&#xff0c;需要將對象轉成用 & 連接的字符串拼接在路徑后面。 實現方法

C++中對SQLite進行增刪改查

#include <iostream> #include <sqlite3.h>// 創建數據庫連接 sqlite3* OpenDatabase(const char* dbFilePath) {sqlite3* db;// 打開數據庫if (sqlite3_open(dbFilePath, &db) ! SQLITE_OK) {std::cerr << "Error opening database." <<…

HTTP ERROR 403 No valid crumb was included in the request

1、報錯截圖&#xff1a; 2、產生原因&#xff1a; 開啟了csrf&#xff0c;即跨站請求偽造 3、新版本不支持頁面修改&#xff0c;故需要修改jenkins配置文件 3.1 進入編輯配置文件 vim /etc/sysconfig/jenkins 3.2 修改JENKINS_JAVA_OPTIONS&#xff0c;并保存修改 JENKI…

深度學習之四(循環神經網絡Recurrent Neural Networks,RNNs)

概念 循環神經網絡(Recurrent Neural Networks,RNNs)是一類專門用于處理序列數據的神經網絡,它在處理時考慮了序列數據的順序和上下文信息。RNNs 在自然語言處理、時間序列分析、語音識別等領域得到廣泛應用。 1. 基本結構: RNN 的基本結構包含一個或多個循環單元,每個…

Ubuntu 系統上使用 QQ 郵箱的 SMTP 服務器發送郵件,msmtp(已驗證)

安裝 msmtp sudo apt-get update sudo apt-get install msmtp2 .配置 msmtp nano ~/.msmtprcdefaults auth on tls on tls_starttls on tls_trust_file /etc/ssl/certs/ca-certificates.crt logfile ~/.msmtp.logaccount qq host …

Lua腳本解決redis實現的分布式鎖多條命令原子性問題

線程1現在持有鎖之后&#xff0c;在執行業務邏輯過程中&#xff0c;他正準備刪除鎖&#xff0c;而且已經走到了條件判斷的過程中&#xff0c;比如他已經拿到了當前這把鎖確實是屬于他自己的&#xff0c;正準備刪除鎖&#xff0c;但是此時他的鎖到期了&#xff0c;那么此時線程2…

Android : ExpandableListView(折疊列表) +BaseExpandableListAdapter-簡單應用

示例圖&#xff1a; 實體類DemoData.java package com.example.myexpandablelistview.entity;public class DemoData {private String content;private int img;public DemoData(String content, int img) {this.content content;this.img img;}public String getContent()…

STM32——外部中斷

文章目錄 0.中斷關系映射1.使能 IO 口時鐘&#xff0c;初始化 IO 口為輸入2.設置 IO 口模式&#xff0c;觸發條件&#xff0c;開啟 SYSCFG 時鐘&#xff0c;設置 IO 口與中斷線的映射關系。3.配置NVIC優先級管理&#xff0c;并使能中斷4.編寫中斷服務函數。5.編寫中斷處理回調函…

springboot多數據源集成

springboot多數據源集成 1、添加依賴2、添加配置3、代碼使用4、動態切換數據庫 1、添加依賴 <!--多數據源--> <dependency><groupId>com.baomidou</groupId><artifactId>dynamic-datasource-spring-boot-starter</artifactId><version…

[個人筆記] Windows的IT運維筆記

IT技術 - 運維篇 第二章 Windows的IT運維筆記 IT技術 - 運維篇系列文章回顧一、Windows10專業版添加gpedit.msc二、海康威視前端頁面導出通道名稱參考鏈接 系列文章回顧 第一章 快速下載微軟評估版本鏡像的方法 一、Windows10專業版添加gpedit.msc 執行以下bat腳本 echo off…

Rust使用iced構建UI時,如何在界面顯示中文字符

注&#xff1a;此文適合于對rust有一些了解的朋友 iced是一個跨平臺的GUI庫&#xff0c;用于為rust語言程序構建UI界面。 iced的基本邏輯是&#xff1a; UI交互產生消息message&#xff0c;message傳遞給后臺的update&#xff0c;在這個函數中編寫邏輯&#xff0c;然后通過…

護法革命:CIMIVO+SOTUY洗前發膜讓發絲重獲“芯”生

愛美之心人皆有之,經常燙染或者是在太陽下暴曬,都會對發絲造成一定的傷害,一旦發絲受損,就會導致發芯內部角蛋白流失、化學鍵連接斷裂,進而出現各種發質問題。為此,日本知名化妝品集團NABOCUL旗下發芯修護引領品牌ENNEO創新研發兩大核心成分:CIMIVO、SOTUY,能夠從根源修護發芯內…

EXCEL小技巧

1、兩列文本合并顯示&#xff1a; CONCATENATE(B6,E6) &#xff08;如果顯示公式而非文本&#xff0c;就是公式輸錯了&#xff0c;比如后缺少空格&#xff09;

mac 終端配置

Mac iTerm2 配置 安裝 brew install iTerm2安裝完成之后&#xff0c;需要重新打開終端&#xff0c;既可以看見安裝 iTerm2 的效果。 iTerm2 美化 使用 oh-my-zsh 美化 iTerm2 終端 安裝 brew install wget sh -c "$(wget https://raw.github.com/ohmyzsh/ohmyzsh/mast…

宇宙工具箱:辦公娛樂兩不誤堪稱手機的百寶箱

宇宙工具箱 宇宙工具箱&#xff0c;提供大量實用工具&#xff0c;多種類型盡在其中&#xff0c;支持圖片/視頻處理、計算、查詢、娛樂等多方內容&#xff0c;滿足用戶使用需求&#xff0c;超多工具直接使用。 獲取資源 詳細獲取地址請點擊 宇宙工具箱 功能特點 1、計算工具…

【硬核HeyGen平替】在window平臺上使用MyHeyGen

最近在研究HeyGen的平替開源項目&#xff0c;然后發現了MyHeyGen這個項目&#xff0c;但是文檔上面并沒有說明如果在window平臺上使用&#xff0c;考慮到非window平臺安裝顯卡驅動什么的比較繁瑣&#xff0c;所以嘗試硬著頭皮干... 前提 開源項目中所需的環境準備要先準備好 1…