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();
} }

Android_Spinner_Listener的更多相关文章

随机推荐

  1. ccr1

    Concurrency and Coordination Runtime Jeffrey Richter Code download available at:ConcurrentAffairs200 ...

  2. GDI+ 学习记录(26): 显示图像 - Image

    //显示图像 var   g: TGPGraphics;   img: TGPImage; begin   g := TGPGraphics.Create(Self.Canvas.Handle);   ...

  3. java基础之数据类型转换

    在写java程序时,经常会遇到需要数据类型转换,下面我们来介绍一些一些基本数据类型之间的转换. 1.int,folat,double,boolean,long 转换成字符串,其实很简单只需使用一个函数 ...

  4. Git 基础 - Git Aliases

    $ git config --global alias.co checkout $ git config --global alias.br branch $ git config --global ...

  5. 【原】实验室签到PHP版本

    表单 <html> <body> <h1>实验室自动签到测试</h1> <h2>输入学号和登录密码(建议自己改过密码后再来录入您的数据)&l ...

  6. Newtonsoft.Json.dll使用

    1:Newtonsoft.Json.dll 下载  http://json.codeplex.com/ 2:解析JSON字符窜 方法1: using Newtonsoft.Json; using Sy ...

  7. 嵌入式开发应该掌握的一些Linux命令

    Linux提供了大量的命令,利用它可以有效地完成大量的工作,如磁盘操作.文件存取.目录操作.进程管理.文件权限设定等.所以,在Linux系统上工作离不开使用系统提供的命令.要想真正理解Linux系统, ...

  8. Tomcat 7 Connector 精读(1)

    这个类图是本人截取的最重要的类的方法和属性. 其中ProtocalHandler是协议处理器,tomcat支持的协议以下方法可以看到.不同协议实现了不同的ProtocalHandler类. publi ...

  9. MAC OS安装wget

    MAC下没有wget工具,不习惯curl,使用起来还是很不方便的.下载了一个wget源码吧,编译安装.sudo curl -O http://ftp.gnu.org/gnu/wget/wget-1.1 ...

  10. hdu 5533 Dancing Stars on Me

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5533 Dancing Stars on Me Time Limit: 2000/1000 MS (Ja ...