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. PHP学习笔记 - 进阶篇(6)

    PHP学习笔记- 进阶篇(6) 会话控制(session与cookie) 当前的Cookie为: cookie简介 Cookie是存储在客户端浏览器中的数据,我们通过Cookie来跟踪与存储用户数据. ...

  2. iOS 字体滚动效果 ScrollLabel

    写了一个简单的字体滚动效果. 用了一种取巧的方式,传入两个一摸一样的Label(当然也可以是别的视图), 话不多说,代码里面讲解. SEScrollLabel.h #import <UIKit/ ...

  3. 一些常用sqlite语句

    1,如果表不存在就新建一个 CComBSTR bstrCreatBat(L”CREATE TABLE IF NOT EXISTS tb_Name (\ rowIdIndex  INTEGER PRIM ...

  4. 怎样把SEL放进NSArray里

    我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3805270.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...

  5. 重回cnblogs

    毕业一年,关于工作的想法和思路渐渐充实,是时候回到cnblogs,开始写技术日志了.

  6. 登堂入室——java流

    ——文章出自PeterYe,不得私自转载 我所知道的 java.io里面的[流],就仿佛太平洋里面的水一样,浩浩荡荡,横无际涯... -----2016/7/16--------公寓处记录------ ...

  7. DTCMS视频模版更改,

    1.去掉style.css中视频的高度 .video-box{ position:relative; width:100%; height:473px; _clear:both; _display:i ...

  8. bootstrap响应式网页设计的9条基本原则!

    bootstrap响应式网页设计是针对多屏幕问题的一个很好的解决方案,但从印刷的视角来看有点困难.没有固定的页面尺寸,没有毫米或英寸,没有任何的物理限制,无从下手. 为了desktop和mobile单 ...

  9. Linux各发行版本 优缺点 简介

    2008.01.21 13:43 Linux最早由Linus Benedict Torvalds在1991年开始编写.在这之前,RichardStallman创建了Free SoftwareFound ...

  10. CorelDRAW 二维码插件

    随着智能手机的流行,二维码在各个领域大量应用,这个插件在补CorelDRAW这方面的不足: 这个插件是 cpg 格式,安装请看这篇博客:http://www.cnblogs.com/o594cql/p ...