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. Clean Code – Chapter 2: Meaningful Names

    Use Intention-Revealing Names The name should tell you why it exists, what it does, and how it is us ...

  2. 数电课设——琐碎

    这几天没有更新过网站了,也没继续开发VellLock了,可是感觉还是没有闲着,一直在跟下面的一些元器件在打交道,当然下面的都是小儿科,英文文档都看得我快吐血了.数电基本属于棺材边上过的我,是各种头大, ...

  3. Linux下安装memcache

    1.Memcache用到了libevent(这个库用于Socket的处理),需要安装libevent: (1)tar zxvf libevent.tar.gz 后进入解压后的文件夹 (2)./conf ...

  4. Solution for latex error:”Unknown graphics extension: .eps“ or "Can not find XXX"

    Sample code: \begin{figure*} \centering % Requires \usepackage{graphicx} \includegraphics[width=7in] ...

  5. HDU 3488--Tour(KM or 费用流)

    因为每个点只能经过一次 所以考虑拆点 这题有坑,有重边.. KM算法 把一个点拆成入点和出点 入点在X部,出点在Y步. 如果u,v之间有路径,就在X部的u点连接Y部的v点 求完美匹配. 当完美匹配的时 ...

  6. Java反射机制练习(增强可扩展性)

    模拟电脑的运行,主板运行,对外提供接口PCI,网卡,声卡等设备实现该接口可以完成指定动作,练习中用到的类Mainboard,NetCard,SoundCard,接口PCI 主板: package Re ...

  7. 获取EXe版本信息

    function GetVersionString(FileName: string): string;    var    VerInfoSize: DWORD;    VerInfo: Point ...

  8. A Tour of Go Concurrency

    The next section covers Go's concurrency primitives. A Tour of Go Goroutines A goroutine is a lightw ...

  9. light oj 1138

      Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Pract ...

  10. hdoj 1402 Prepared for New Acmer【快速幂】

    Prepared for New Acmer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...