MainActivity.java
MyData.java
activity_main.xml
package com.ct7_Sprinner;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView textView;
private Spinner spinner;
private List<String> list=null;
private ArrayAdapter<String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySet();
list=MyData.getData();
setArrayAdapter();
spinner.setAdapter(adapter);
}
private void mySet() {
// TODO Auto-generated method stub
textView=(TextView) findViewById(R.id.textView_Show);
spinner=(Spinner) findViewById(R.id.spinner_City);
spinner.setOnItemSelectedListener(itemSelecedtListener);
}
private OnItemSelectedListener itemSelecedtListener=new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
textView.setText("您选择的是:"+adapter.getItem(position));
parent.setVisibility(View.VISIBLE);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
textView.setText("NONE");
parent.setVisibility(View.VISIBLE);
}
};
private void setArrayAdapter() {
// TODO Auto-generated method stub
adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
}
}
MyData.java
package com.ct7_Sprinner;
import java.util.ArrayList;
import java.util.List;
public class MyData {
public static List<String> getData() {
List<String> list=new ArrayList<String>();
list.add("北京");
list.add("上海");
list.add("深圳");
list.add("南京");
list.add("东莞");
list.add("南山");
list.add("广州");
list.add("重庆");
list.add("天津");
return list;
}
}
activity_main.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" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widge28"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView_Show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="您的选择是"
android:textSize="25sp" />
<Spinner
android:id="@+id/spinner_City"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>