xml布局文件:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_notity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/spinner"
android:layout_marginTop="10dp"
android:text="更新列表"
android:textSize="20sp"/> </RelativeLayout>
spinner_item文件:
<?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/iamge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/> </LinearLayout>
源代码:
package com.example.day04_spinnerlistener; import java.util.ArrayList; 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.Button;
import android.widget.Spinner; public class MainActivity extends Activity implements View.OnClickListener{ private Spinner spinner;
private String TAG = "MainActivity";
private String[] cities;
private ArrayAdapter<String> adapter;
private Button button;
private ArrayList<String> cityList; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.btn_notity);
button.setOnClickListener(this);
spinner = (Spinner) findViewById(R.id.spinner);
cities = new String[] {"北京","上海","广州","杭州","天津"};
cityList = new ArrayList<String>();
for (int i = 0; i < cities.length; i++) {
cityList.add(cities[i]);
}
adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_dropdown_item_1line, cityList);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
//如何获取被点击的item对应的数据
//通过数据源数组获得该索引的数据
String name1 = cities[position];
//通过适配器获取索引条目的数据
String name2 = adapter.getItem(position);
//通过spinner(适配器控件)获取该索引位置条目的数据
String name3 = spinner.getItemAtPosition(position).toString();
Log.i(TAG ,position+"name1:"+name1+"name2,"+name2+",name3:"+name3 );
} @Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub }
}); }
/**
* 当点击更新按钮,触发监听事件,执行更新数据的操作
*/
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ArrayList<String> newList = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
newList.add("item "+i);
}
//清空数据
// cityList.removeAll(cityList);
//添加新的数据到原数据中
cityList.addAll(newList);
//通知更新数据
adapter.notifyDataSetChanged();
} }