代码如下:
package com.lixu.xialakuang; import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView; public class MainActivity extends Activity {
TextView tv2;
TextView tv3; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); final String[] start = this.getResources().getStringArray(R.array.start);
final String[] target = this.getResources().getStringArray(R.array.target); Spinner spinner1 = (Spinner) findViewById(R.id.sp1);
Spinner spinner2 = (Spinner) findViewById(R.id.sp2); tv2 = (TextView) findViewById(R.id.tv2);
tv3 = (TextView) findViewById(R.id.tv3); tv2.setTextSize(20);
tv3.setTextSize(20); MyAdapterr mMyAdapterr = new MyAdapterr(this, -1, target); MyAdapterr mMyAdapter = new MyAdapterr(this, -1, start); spinner1.setAdapter(mMyAdapter); spinner2.setAdapter(mMyAdapterr); spinner1.setOnItemSelectedListener(new OnItemSelectedListener() { @Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
tv2.setText(target[arg2]);
} @Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
spinner2.setOnItemSelectedListener(new OnItemSelectedListener() { @Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
tv3.setText(start[arg2]);
} @Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}); } private class MyAdapterr extends ArrayAdapter {
LayoutInflater flater;
private Context context;
private String[] target;
int[] image = { R.drawable.beijing, R.drawable.chengdu, R.drawable.guangzhou, R.drawable.hangzhou,
R.drawable.wuhan, R.drawable.xian, R.drawable.shenzhen }; @Override
public int getCount() { return target.length;
} public MyAdapterr(Context context, int resource, String[] target) {
super(context, resource, target);
this.context = context;
this.target = target;
this.image = image;
flater = LayoutInflater.from(context);
} // 设置最上面选中列表的view
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = flater.inflate(R.layout.list, null);
ImageView iv = (ImageView) convertView.findViewById(R.id.iv1);
iv.setImageResource(image[position]); TextView tv = (TextView) convertView.findViewById(R.id.tv1);
tv.setBackgroundColor(Color.RED); tv.setText(target[position]);
return convertView;
} // 设置下拉列表的 view 如果不写这个方法会报 Resources$NotFoundException: Resource ID
// #0xffffffff这个错误 @Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = flater.inflate(R.layout.list, null);
ImageView iv = (ImageView) convertView.findViewById(R.id.iv1);
iv.setImageResource(image[position]); TextView tv = (TextView) convertView.findViewById(R.id.tv1); tv.setBackgroundColor(Color.GREEN);
tv.setText(target[position]);
return convertView;
}
} private class MyAdapter extends ArrayAdapter { LayoutInflater flater;
private Context context;
private String[] start;
int[] image = { R.drawable.beijing, R.drawable.chengdu, R.drawable.guangzhou, R.drawable.hangzhou,
R.drawable.wuhan, R.drawable.xian, R.drawable.shenzhen }; @Override
public int getCount() { return start.length;
} public MyAdapter(Context context, int resource, String[] target) {
super(context, resource, target);
this.context = context;
this.start = start;
flater = LayoutInflater.from(context);
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = flater.inflate(R.layout.list, null); ImageView iv = (ImageView) convertView.findViewById(R.id.iv1);
iv.setImageResource(image[position]); TextView tv = (TextView) convertView.findViewById(R.id.tv1);
tv.setBackgroundColor(Color.RED);
tv.setText(start[position]); return convertView;
} @Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = flater.inflate(R.layout.list, null);
ImageView iv = (ImageView) convertView.findViewById(R.id.iv1);
iv.setImageResource(image[position]);
TextView tv = (TextView) convertView.findViewById(R.id.tv1); tv.setText(start[position]);
tv.setBackgroundColor(Color.GREEN); return convertView;
}
}
}
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:orientation="horizontal" > <ImageView
android:id="@+id/iv1"
android:layout_width="50dp"
android:layout_height="50dp" /> <TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="10dp"
android:textSize="20sp" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources> <string-array name="start">
<item>北京</item>
<item>成都</item>
<item>广州</item>
<item>杭州</item>
<item>武汉</item>
<item>西安</item>
<item>深圳</item>
</string-array>
<string-array name="target">
<item>北京</item>
<item>成都</item>
<item>广州</item>
<item>杭州</item>
<item>武汉</item>
<item>西安</item>
<item>深圳</item>
</string-array> </resources>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.lixu.xialakuang.MainActivity" > <TextView
android:id="@+id/tv2"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:gravity="center" /> <TextView
android:id="@+id/tv3"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_toRightOf="@id/tv2"
android:gravity="center" /> <LinearLayout
android:id="@+id/linearlayout"
android:layout_width="300dp"
android:layout_height="200dp"
android:layout_below="@+id/tv2"
android:layout_centerHorizontal="true"
android:orientation="horizontal" > <Spinner
android:id="@+id/sp1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" /> <Spinner
android:id="@+id/sp2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout> </RelativeLayout>
运行效果图: