/* simpleadapteractivity_main.xml,这个是自定义的下拉列表布局,用来给activity_main.xml 布局的下拉列表显示图片跟选项*/
public class SimpleApapterfMainActivity extends Activity {
private Spinner sp;
private TextView txt;
private SimpleAdapter simpleadapter;
private int []image={R.drawable.bump,R.drawable.calculator,R.drawable.music,R.drawable.radio,R.drawable.rss,R.drawable.score
,R.drawable.sky,R.drawable.talk};
private String[]str={"小学","初中","高中","大学","研究生","硕士","博士","博士后"};//数据源
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp=(Spinner) this.findViewById(R.id.sp);
txt=(TextView) this.findViewById(R.id.txt);
//data是map键值对的数据,自定义类型要用自己定义数据
List<Map<String, Object>> data=new ArrayList<Map<String,Object>>();
//将前面定义的数组数据,装载到data里面
for(int i=0;i<image.length;i++ ){
Map<String ,Object>itemMap=new HashMap<String ,Object>();
itemMap.put("header", image[i]);
itemMap.put("name", str[i]);
//这里的"header"和"name"是随便给的,只是到时候取值要记得自己的键去取值
data.add(itemMap);
}
/**
* SimpleAdapter(context, data, resource, from, to)
* context:上下文
* data:数据是map键值对,加到list集合中
* resource:自定义的布局,现在用的安卓自带的显示布局模式:android.R.layout.simple_list_item_1
* from:字符串类型的键
* to:整形的自定义布局,即控件的id
*/
//将自己定义的布局给适配器:自己建一个布局文件R.layout.simpleadapteractivity_main
simpleadapter=new SimpleAdapter(this, data, R.layout.simpleadapteractivity_main, new String[]{"header","name"}, new int[]{R.id.header,R.id.txt});
sp.setAdapter(simpleadapter);
//设置监听
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
//怎么获取自定义的内容
//通过适配器获取该位置的内容,也就是键key,再根据键key获取内容键值
Map<String ,Object>item=(Map<String, Object>) simpleadapter.getItem(position);
Log.i("simpleAd", item+"");
txt.setText(item.get("name")+"");
Log.i("simpleAd", item.get("name")+"");
/* //适配器获取点击的内容, ArrayAdapter
//法1:
String data1=sp.getSelectedItem().toString();
//法2:
String data2=parent.getSelectedItem().toString();
//法3:
String data3=parent.getItemAtPosition(position).toString();
//法4:
//String data4=adapter.getItem(position);
//txt.setText(data1+","+data2+","+data3+","+data4);
*/ }
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
public void submit(View view){
String data=sp.getSelectedItem().toString();
txt.setText(data);
}
}
//主布局文件
<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"
>
<!-- 下拉列表 -->
<Spinner
android:id="@+id/sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交"
android:onClick="submit"
/>
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
/>
</LinearLayout>
//自定义布局文件
<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="horizontal"
>
<!-- 这个是自定义的下拉列表布局,用来给activity_main.xml 布局的下拉列表显示图片跟选项-->
<!-- 这是跟选项显示在下拉列表中的,所以图片要小一下 android:scaleType="centerInside"根据图片大小居中,不会拉伸 -->
<ImageView
android:id="@+id/header"
android:layout_width="40dp"
android:layout_height="40dp"
android:scaleType="centerInside"
/>
<TextView
android:id="@+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:gravity="center"
/>
</LinearLayout>