android spinner下拉列表

MainActivity.java


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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值