ドロップダウン ボックスの使用法とアダプターの基本概念を、ドロップダウン ボックス Spinner の使用説明と組み合わせて、それぞれ、アレイ アダプター ArrayAdapter と簡易アダプター SimpleAdapter の具体的な使用法と表示効果を説明します。
1. ドロップダウン ボックス スピナー
1. スピナーはリストの文字列から項目を選択するために使用され、その機能はラジオ ボタンの組み合わせに似ています。
2. XML ファイルの spinnerMode 属性には 2 つの値があります。
ドロップダウン: ドロップダウン リスト フォーム
ダイアログ: ダイアログフォーム
3. Javaコード内で呼び出せるメソッドは以下の4つです。
setPrompt: タイトルテキストを設定します。
setAdapter: ドロップダウン リストのアダプターを設定します。
setSelection: 現在選択されている項目を設定します。
setOnItemSelectedListener: ドロップダウン リストの選択リスナーを設定します。
2. アレイアダプター
ドロップダウン ボックスは setAdapter メソッドを呼び出してリスト アダプターを設定します。最も単純なアダプターは配列アダプターです。
アレイ アダプターの使用は、次の手順に分かれています。
(1) リスト項目の XML ファイルを記述し、内部レイアウトには TextView タグが 1 つだけあります
(2) ArrayAdapterの構築メソッドを呼び出し、表示したい文字列配列とリスト項目のXMLファイル(R.layout.item_select)を記入します。
新しい ArrayAdapter(コンテキスト、リソース、textViewResourceId、オブジェクト);
context: context、これを書くだけです。
リソース: サブレイアウト項目
textViewResourceId: レイアウト内の textView のコンテンツを適応させるコントロールの ID。
オブジェクト:データ ソース データ
(3) ドロップダウン ボックス コントロールの setAdapter メソッドを呼び出し、2 番目の手順で取得したアダプター インスタンスを渡します。
package com.example.chapter05;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class SpinnerDropdownActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner_dropdown);
initSpinnerForDropdown(); // 初始化下拉模式的列表框
}
// 初始化下拉模式的列表框
private void initSpinnerForDropdown() {
// 声明一个下拉列表的数组适配器
ArrayAdapter<String> starAdapter = new ArrayAdapter<String>(this,
R.layout.item_select, starArray);
// 从布局文件中获取名叫sp_dropdown的下拉框
Spinner sp_dropdown = findViewById(R.id.sp_dropdown);
// 设置下拉框的标题。对话框模式才显示标题,下拉模式不显示标题
sp_dropdown.setPrompt("请选择行星");
sp_dropdown.setAdapter(starAdapter); // 设置下拉框的数组适配器
sp_dropdown.setSelection(0); // 设置下拉框默认显示第一项
// 给下拉框设置选择监听器,一旦用户选中某一项,就触发监听器的onItemSelected方法
sp_dropdown.setOnItemSelectedListener(new MySelectedListener());
}
// 定义下拉列表需要显示的文本数组
private String[] starArray = {"水星", "金星", "地球", "火星", "木星", "土星"};
// 定义一个选择监听器,它实现了接口OnItemSelectedListener
class MySelectedListener implements OnItemSelectedListener {
// 选择事件的处理方法,其中arg2代表选择项的序号
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Toast.makeText(SpinnerDropdownActivity.this, "您选择的是" + starArray[arg2],
Toast.LENGTH_LONG).show();
}
// 未选择时的处理方法,通常无需关注
public void onNothingSelected(AdapterView<?> arg0) {}
}
}
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="50dp"
android:singleLine="true"
android:gravity="center"
android:textSize="17sp"
android:textColor="#0000ff" />
<?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"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="下拉模式的列表框"
android:textColor="@color/black"
android:textSize="17sp"/>
<Spinner
android:id="@+id/sp_dropdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dropdown"/>
</LinearLayout>
8.1.3 シンプルアダプター SimpleAdapter
ArrayAdapter はテキスト リストのみを表示できますが、SimpleAdapter ではリスト項目にテキストと画像の両方を表示できます。
単純なアダプターを使用するには、テキスト配列と画像配列を同時に指定する必要があります。次は、SimpleAdapter を使用するためのコード例です。 // ドロップダウン リストの単純なアダプターを宣言します。アイコンとテキストのデータ
SimpleAdapter starAdapter = new SimpleAdapter(this, list,
R.layout.item_simple, new String[]{"アイコン", "名前"},
new int[]{R.id.iv_icon, R.id.tv_name});
sp.setAdapter(starAdapter); // ドロップダウン ボックスの単純なアダプターを設定します
package com.example.chapter05;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SpinnerIconActivity extends AppCompatActivity {
// 定义下拉列表需要显示的行星图标数组
private Object[] iconArray = {R.drawable.icon_point_c, R.drawable.list_selector,
R.drawable.icon_point_c, R.drawable.icon_point_c, R.drawable.icon_point_c,
R.drawable.shape_oval_red};// 定义下拉列表需要显示的行星名称数组
private String[] starArray = {"水星", "金星", "地球", "火星", "木星", "土星"};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner_icon);
initSpinnerForSimpleAdapter(); // 初始化下拉框,演示简单适配器
}
private void initSpinnerForSimpleAdapter(){
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 starAdapter = new SimpleAdapter(this, list,
R.layout.item_simple, new String[]{"icon", "name"},
new int[]{R.id.iv_icon, R.id.tv_name});
starAdapter.setDropDownViewResource(R.layout.item_simple);
// 从布局文件中获取名叫sp_icon的下拉框
Spinner sp_icon = findViewById(R.id.sp_icon);
sp_icon.setPrompt("请选择行星"); // 设置下拉框的标题
sp_icon.setAdapter(starAdapter); // 设置下拉框的简单适配器
sp_icon.setSelection(0); // 设置下拉框默认显示第一项
// 给下拉框设置选择监听器,一旦用户选中某一项,就触发监听器的onItemSelected方法
sp_icon.setOnItemSelectedListener(new MySelectedListener());
}
// 定义一个选择监听器,它实现了接口OnItemSelectedListener
class MySelectedListener implements AdapterView.OnItemSelectedListener {
// 选择事件的处理方法,其中arg2代表选择项的序号
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Toast.makeText(SpinnerIconActivity.this, "您选择的是" + starArray[arg2], Toast.LENGTH_LONG).show();
}
// 未选择时的处理方法,通常无需关注
public void onNothingSelected(AdapterView<?> arg0) {}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- 这是展示行星图标的ImageView -->
<ImageView
android:id="@+id/iv_icon"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1" />
<!-- 这是展示行星名称的TextView -->
<TextView
android:id="@+id/tv_name"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:gravity="center"
android:textColor="#ff0000"
android:textSize="17sp" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="行星的简单适配器"
android:textColor="@color/black"
android:textSize="17sp" />
<Spinner
android:id="@+id/sp_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dialog" />"
</LinearLayout>