首页 > 代码库 > Android Spinner(级联 天气预报)
Android Spinner(级联 天气预报)
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(); } } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。