<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<Spinner
android:id="@+id/spinner1"
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"
android:layout_marginTop="10sp" >
</Spinner>
</LinearLayout>
<?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="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageview1"
android:layout_width="60sp"
android:layout_height="60sp"
android:paddingLeft="10sp"
/>
<TextView
android:id="@+id/textview1"
android:layout_width="60sp"
android:layout_height="60sp"
android:layout_gravity="center"
/>
</LinearLayout>
package com.example.data;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.example.android_spinner.R;
public class AdapterData {
public AdapterData() {
// TODO Auto-generated constructor stub
}
public static List<String> getListData() {
List<String> list = new ArrayList<String>();
list.add("中国 ");
list.add("美国 ");
list.add("俄罗斯");
return list;
}
public static List<Map<String, Object>> getListmapData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map map1 = new HashMap<String, Object>();
map1.put("国旗", R.drawable.pic1);
map1.put("国家", "中国");
list.add(map1);
Map map2 = new HashMap<String, Object>();
map2.put("国旗", R.drawable.pic3);
map2.put("国家", "美国");
list.add(map2);
return list;
}
}
package com.example.android_spinner;
import java.util.HashMap;
import com.example.data.AdapterData;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
public class MainActivity extends Activity {
private Spinner spinner1;
private Spinner spinner2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item,AdapterData.getListData());
spinner1.setAdapter(arrayAdapter);
SimpleAdapter simpleAdapter=new SimpleAdapter(MainActivity.this, AdapterData.getListmapData(), R.layout.item, new String[]{"国旗","国家"}, new int[]{R.id.imageview1,R.id.textview1});
spinner2.setAdapter(simpleAdapter);
spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
HashMap<String,String> map=(HashMap<String, String>) spinner2.getSelectedItem();
String str=map.get("国家");
Log.i("Main", str);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}