activity_spinner.xml

<?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="vertical" > <Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> <Spinner
android:id="@+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> <Spinner
android:id="@+id/spinner3"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> <TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" /> </LinearLayout>

SpinnerActivity.java

package com.wangzhu.demospinner;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List; import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView; public class SpinnerActivity extends Activity { private Spinner spinner1, spinner2, spinner3;
private TextView textView1; private ArrayAdapter<String> adapter1, adapter2, adapter3;
private List<String> listData1, listData2, listData3;
private ProgressDialog dialog; private String downloadData; private MyThread myThread1, myThread2, myThread3, myThread4, myThread5;
private MyHandler myHandler;
private String preUri = "http://m.weather.com.cn/data";
private String uri; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner); textView1 = (TextView) findViewById(R.id.textView1);
listData1 = new ArrayList<String>();
listData2 = new ArrayList<String>();
listData3 = new ArrayList<String>(); adapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, listData1);
adapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, listData2);
adapter3 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, listData3); spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) { Log.d("TAG", "spinner1 " + position + "=====" + id);
uri = preUri + String.format("5/city%02d", position + 1)
+ ".xml";
init2();
} @Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
spinner1.setAdapter(adapter1); spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Log.d("TAG", "spinner2 " + position + "=====" + id);
uri = preUri
+ String.format("5/city%02d",
spinner1.getSelectedItemId() + 1)
+ String.format("%02d", position + 1) + ".xml";
init3();
} @Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
spinner2.setAdapter(adapter2); spinner3 = (Spinner) findViewById(R.id.spinner3);
spinner3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Log.d("TAG", "spinner3 " + position + "=====" + id);
uri = preUri
+ String.format("5/city%02d",
spinner1.getSelectedItemId() + 1)
+ String.format("%02d",
spinner2.getSelectedItemId() + 1)
+ String.format("%02d", position + 1) + ".xml";
init4();
} @Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
spinner3.setAdapter(adapter3); myHandler = new MyHandler();
myThread1 = new MyThread(1);
myThread2 = new MyThread(2);
myThread3 = new MyThread(3);
myThread4 = new MyThread(4);
myThread5 = new MyThread(5);
init1();
} private void init1() {
showDialog("温馨提示", "正在加载第一级目录,请稍候。。。");
uri = "http://m.weather.com.cn/data5/city.xml";
new Thread(myThread1).start();
} private void init2() {
showDialog("温馨提示", "正在更新第二级目录,请稍候。。。");
new Thread(myThread2).start();
} private void init3() {
showDialog("温馨提示", "正在更新第三季目录,请稍后。。。");
new Thread(myThread3).start();
} private void init4() {
showDialog("温馨提示", "正在更新天气,请稍候。。。");
new Thread(myThread4).start();
} private void init5() {
showDialog("温馨提示", "正在更新天气,请稍候。。。");
new Thread(myThread5).start();
} private void showDialog(String title, String msg) {
dialog = new ProgressDialog(SpinnerActivity.this);
dialog.setTitle(title);
dialog.setMessage(msg);
dialog.show();
} private void download(String uri) {
downloadData = null;
try {
URL url = new URL(uri);
// 使用HttpURLConnection打开连接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(2000);
conn.setUseCaches(false);
if (HttpURLConnection.HTTP_OK == conn.getResponseCode()) {
InputStream in = conn.getInputStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(in));
StringBuffer result = new StringBuffer();
String readLine = null;
while ((readLine = reader.readLine()) != null) {
result.append(readLine);
}
// 关闭InputStream
in.close();
// 关闭http连接
conn.disconnect();
downloadData = result.toString();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} class MyThread implements Runnable {
private int flag; public MyThread(int flag) {
this.flag = flag;
} @Override
public void run() {
// Looper.prepare();
download(uri);
myHandler.sendEmptyMessage(flag);
}
} class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Log.d("TAG", uri);
switch (msg.what) {
case 1:
if (null != downloadData) {
Log.d("TAG", downloadData);
String[] dataArr = downloadData.split(",");
for (int i = 0, len = dataArr.length; i < len; i++) {
listData1.add(dataArr[i].substring(3));
}
// adapter1.notifyDataSetChanged(); adapter1 = new ArrayAdapter<String>(SpinnerActivity.this,
android.R.layout.simple_spinner_item, listData1);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1); }
break;
case 2:
if (null != downloadData) {
Log.d("TAG", downloadData);
String[] dataArr = downloadData.split(",");
listData2.clear();
for (int i = 0, len = dataArr.length; i < len; i++) {
listData2.add(dataArr[i].substring(5));
}
// adapter2.notifyDataSetChanged(); adapter2 = new ArrayAdapter<String>(SpinnerActivity.this,
android.R.layout.simple_spinner_item, listData2);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2); }
break;
case 3:
if (null != downloadData) {
Log.d("TAG", downloadData);
String[] dataArr = downloadData.split(",");
listData3.clear();
for (int i = 0, len = dataArr.length; i < len; i++) {
listData3.add(dataArr[i].substring(7));
}
// adapter3.notifyDataSetChanged(); adapter3 = new ArrayAdapter<String>(SpinnerActivity.this,
android.R.layout.simple_spinner_item, listData3);
adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner3.setAdapter(adapter3); }
break;
case 4:
if (null != downloadData) {
Log.d("TAG", downloadData);
dialog.dismiss();
uri = preUri + "/" + downloadData.substring(7) + ".html";
init5();
}
break;
case 5:
if (null != downloadData) {
Log.d("TAG", downloadData);
textView1.setText(downloadData);
}
break;
}
dialog.dismiss();
}
}
}

Android Spinner(级联 天气预报)的更多相关文章

  1. android Spinner 续

    android Spinner 续 动态增删Spinner中的数据项 public class EX04_09 extends Activity{  private static final Stri ...

  2. Xamarin android spinner的使用方法

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  3. Android spinner 样式及其使用详解

    设计与开发首页 > 应用专题 > 移动开发 > 正文> Android spinner 样式及其使用详解 相关文章: Android 开源项目应用程序与框架推荐 Android ...

  4. Android Spinner In Toolbar

    As the title of the post suggest in this tutorial we will see how to have spinner widget inside the ...

  5. 基于Android开发的天气预报app(源码下载)

    原文:基于Android开发的天气预报app(源码下载) 基于AndroidStudio环境开发的天气app -系统总体介绍:本天气app使用AndroidStudio这个IDE工具在Windows1 ...

  6. Android Spinner 下拉框简单应用 详细注解

    目录 Android Spinner 代码部分 Spinner代码介绍 核心代码 说在最后 @ Android Spinner Spinner 提供下拉列表式的输入方式,该方法可以有效节省手机屏幕上的 ...

  7. android Spinner的简单用法

    上代码: spinner = (Spinner) findViewById(R.id.spinner); tv = (TextView) findViewById(R.id.tv); final Ar ...

  8. Android spinner控件

    spinner控件是Android中下拉控件,现在介绍它两种用法.第一种,从资源文件中获取下拉值:第二种,从代码中获取下拉值. 第一种,首先要在资源文件中把值写好: <?xml version= ...

  9. android Spinner的使用

    首先是MainActivity package com.example.spinnertest; import java.util.ArrayList; import java.util.List; ...

随机推荐

  1. iOS动画——文字晃动

    比如密码错误提示,需要文字左右晃动几次,以起到警示的作用. 为了调用使用方便,做一个分类.代码如下: .h文件 #import <QuartzCore/QuartzCore.h> @int ...

  2. css笔记——杂记

    ####文本框内阴影#### input[type=text], textarea, input[type=email], input[type=url]{ box-shadow: inset 2px ...

  3. C++多线程技术windows常用方法

    随着计算机CPU计算能力快速提高,计算机的处理性能和并行性能力也大大提升.那么,一味使用运行时标准库的C++语言也应该开始支持多线程技术.今天,我为大家带来了C++在windows平台下的常用多线程方 ...

  4. C# 代理HTTP请求

    目的: 应该有不少人需要去某些网站不停爬取数据,有时会使用HTTPRequest一直请求某个网站的某个网址.有的网站比如 QQ空间,赶集网(这是我测试的网站),不停刷新会提醒你的账号异常,可能会查封你 ...

  5. PHP 魔术方法 __get __set (二)

    慢慢长寻夜,明月高空挂 __get() - 读取不可访问属性的值时,__get() 会被调用 __set() - 在给不可访问属性赋值时,__set() 会被调用 <?php /** * 清晰的 ...

  6. 进度条轮播【BackgroundColor】

    直接贴代码先看 HTML: <div class="bannar"> <div class="img"> <ul> < ...

  7. 【转】Qt使用自带的windeployqt 生成exe来发布软件

    集成开发环境 QtCreator 目前生成图形界面程序 exe 大致可以分为两类:Qt Widgets Application  和 Qt Quick Application.下面分别介绍这两类exe ...

  8. WeX5与阿里内测的Weex与有何纠葛?快来看HTML5开发圈那些逗逼事儿!

    4月21日~23日,由infoQ主办的2016 Qcon大会北京站如期举行. HTML5开发已经成为移动开发/前端专题中无可争议的焦点,核心议题已经由前几年的是否该用HTML5转向了如何高性能.高效率 ...

  9. linux之cat命令

    1. cat 接普通文件名,会把文件内容打印到屏幕:2. cat > file,这个可以向文件“file”写入内容,最后按 Ctrl + D 结束输入,会将你输入的数据保存到文件. cat主要有 ...

  10. php中封装的curl函数(抓取数据)

    介绍一个封闭好的函数,封闭了curl函数的常用步骤,方便抓取数据. 代码如下: <?php /** * 封闭好的 curl函数 * 用途:抓取数据 * edit by www.jbxue.com ...