代码
package com.example.spinner;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import butterknife.Bind;
import butterknife.ButterKnife;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
@Bind(R.id.tv)
TextView tv;
@Bind(R.id.spinner)
Spinner spinner;
@Bind(R.id.spinner2)
Spinner spinner2;
private List<String> list;
private ArrayAdapter<String> stringArrayAdapter;
private List<Map<String, Object>> mData;
private SimpleAdapter simpleAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
list = new ArrayList<>();
list.add("北京");
list.add("济南");
list.add("苏州");
list.add("菏泽");
tv.setText("您选择的城市是北京");
initSpinner1();
mData=new ArrayList<>();
initData();
simpleAdapter = new SimpleAdapter(this, mData, R.layout.item_spinner2, new String[]{"image", "text"}, new int[]{R.id.iv_item, R.id.tv_item});
simpleAdapter.setDropDownViewResource(R.layout.item_spinner2);
spinner2.setAdapter(simpleAdapter);
spinner2.setOnItemSelectedListener(this);
}
private void initData() {
for (int i = 0; i < list.size(); i++) {
HashMap<String, Object> objectObjectHashMap = new HashMap<>();
objectObjectHashMap.put("image",R.mipmap.ic_launcher);
objectObjectHashMap.put("text",list.get(i));
mData.add(objectObjectHashMap);
}
}
private void initSpinner1() {
// 新建ArrayAdapter 数组适配器
stringArrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, list);
// adapter设置一个下拉列表样式
stringArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(stringArrayAdapter);
// spinner.setOnItemClickListener(this);
spinner.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (parent.getId()) {
case R.id.spinner:
String cityName = stringArrayAdapter.getItem(position);
tv.setText("您选择的城市是" + cityName);
break;
case R.id.spinner2:
Map<String, Object> stringObjectMap = mData.get(position);
String text = (String) stringObjectMap.get("text");
tv.setText("您选择的城市是" + text);
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
//布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.spinner.MainActivity">
<TextView
android:id="@+id/tv"
android:textSize="25sp"
android:textColor="@android:color/holo_red_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"></Spinner>
<Spinner
android:id="@+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"></Spinner>
</LinearLayout>
//条目布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>