ArrayAdapter主要用于每行列表只显示文本的情况,而SimpleAdapter则还可以给列表加上图标,允许在列表项中展示多个控件。
下面以下拉框Sipnner为例,说明SimpleAdapter的用法。
layout文件夹里的activity_spinner.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"
android:padding="15dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请选择:"
android:textSize="26sp"
android:gravity="center"
android:paddingBottom="10dp"/>
<Spinner
android:id="@+id/sp_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dialog" />"
</LinearLayout>
对应的SpinnerActivity类
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SpinnerActivity extends AppCompatActivity {
//iconArray是行星的图标数组,starArray是行星的名称数组
private int[] iconArray = {R.drawable.shuixing, R.drawable.jinxing, R.drawable.diqiu,
R.drawable.huoxing, R.drawable.muxing, R.drawable.tuxing};
private String[] starArray = {"水星", "金星", "地球", "火星", "木星", "土星"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner);
//声明一个映像对象的队列,用于保存行星的图标与名称配对信息
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (int i = 0; i < iconArray.length; i++) {
Map<String, Object> item = new HashMap<String, Object>();
item.put("icon", iconArray[i]);
item.put("name", starArray[i]);
list.add(item);
}
//声明一个下拉列表的简单适配器,其中制定了图标与文本两组数据
SimpleAdapter starAdapter = new SimpleAdapter(this, list,
R.layout.item_select, new String[] { "icon", "name" },
new int[] {R.id.iv_icon, R.id.tv_name});
//设置简单适配器的布局样式
starAdapter.setDropDownViewResource(R.layout.item_simple);
//从布局文件中获取名叫sp_icon的下拉框
Spinner sp = (Spinner) findViewById(R.id.sp_icon);
//设置下拉框的标题
sp.setPrompt("请选择行星");
//设置下拉框的简单适配器
sp.setAdapter(starAdapter);
//设置下拉框默认显示的第一项
sp.setSelection(0);
//给下拉框设置选择监听器,一旦用户选中某一项,就触发监听器的onItemSelected方法
sp.setOnItemSelectedListener(new MySelectedListener());
}
class MySelectedListener implements AdapterView.OnItemSelectedListener {
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Toast.makeText(SpinnerActivity.this, "您选择的是"+starArray[arg2], Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
}
}
}
layout文件夹里的item_select.xml和item_simple.xml。分别用于设置选中item的样式和下拉框的样式。
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:gravity="center"
android:textSize="17sp"
android:textColor="#0000ff" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/iv_icon"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="center" />
<TextView
android:id="@+id/tv_name"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:gravity="center"
android:textSize="17sp"
android:textColor="#ff0000" />
</LinearLayout>
在此分析一下SimpleAdapter的源码
public class SimpleAdapter extends BaseAdapter implements Filterable, ThemedSpinnerAdapter{
private final LayoutInflater mInflater;
private int[] mTo;
private String[] mFrom;
private ViewBinder mViewBinder;
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
private List<? extends Map<String, ?>> mData;
private int mResource;
private int mDropDownResource;
/** Layout inflater used for {@link #getDropDownView(int, View, ViewGroup)}. */
private LayoutInflater mDropDownInflater;
private SimpleFilter mFilter;
private ArrayList<Map<String, ?>> mUnfilteredData;
public SimpleAdapter(Context context, List<? extends Map<String, ?>> data,
@LayoutRes int resource, String[] from, @IdRes int[] to) {
mData = data;
mResource = mDropDownResource = resource;
mFrom = from;
mTo = to;
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
.............
public void setDropDownViewResource(int resource) {
mDropDownResource = resource;
}
.............
}
可以看出,SimpleAdapter类继承了BaseAdapter抽象类,实现了Filterable和ThemedSpinnerAdapter接口。而BaseAdapter类又实现了ListAdapter和SpinnerAdapter接口。
关于该类的构造方法,一共有五个参数。
/** * Constructor * * @param context The context where the View associated with this SimpleAdapter is running * @param data A List of Maps. Each entry in the List corresponds to one row in the list. The * Maps contain the data for each row, and should include all the entries specified in * "from" * @param resource Resource identifier of a view layout that defines the views for this list * item. The layout file should include at least those named views defined in "to" * @param from A list of column names that will be added to the Map associated with each * item. * @param to The views that should display column in the "from" parameter. These should all be * TextViews. The first N views in this list are given the values of the first N columns * in the from parameter. */
context:上下文;
data:需要展示的数据源,是map组成的list集合,每个Map对应ListView中的一行,每一个Map(键——值对)中的键名必须包含在from中所指定的键;
resource:展示的布局文件,可以自己在layout设置xml文件去设置需要展示 的样式;
from:Map中的键名;
to:绑定数据视图的ID,与from成对应的关系。
第二个参数是关于Map集合的List集合。定义为List<Map<String,Object>>。
new了一个starAdapter对象之后,便可以进行简单适配器的布局样式的设置。以及对下拉框的各种属性进行设置。
注意:setSelection方法要在setAdapter方法后调用。
运行效果如下:
资料来源:Android Studio开发实战