实例介绍
【实例截图】
【核心代码】
java代码:
package com.weather;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.xmlpull.v1.XmlPullParserException;
import com.weather.entity.City;
import com.weather.entity.District;
import com.weather.entity.Province;
import com.weather.json.entity.Result;
import com.weather.json.entity.Weather;
import com.weather.json.entity.Weather_data;
import com.weather.tool.HttpUtils;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends Activity {
private Spinner sp_province;
private Spinner sp_city;
private Spinner sp_district;
private int currentPro;
private ArrayAdapter<Province> provinceAdapter;
private ArrayAdapter<City> cityAdapter;
private ArrayAdapter<District> districtAdapter;
private List<Province> provinces;
private TextView tvCity;
private TextView tvPM25;
private TextView tvDate;
private ImageView ivpic11;
private ImageView ivpic12;
private TextView tvweek1;
private TextView tvwea1;
private TextView tvwind1;
private TextView tvtemper1;
private ImageView ivpic21;
private ImageView ivpic22;
private TextView tvweek2;
private TextView tvwea2;
private TextView tvwind2;
private TextView tvtemper2;
private ImageView ivpic31;
private ImageView ivpic32;
private TextView tvweek3;
private TextView tvwea3;
private TextView tvwind3;
private TextView tvtemper3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp_province = (Spinner) findViewById(R.id.spinner1);
sp_city = (Spinner) findViewById(R.id.spinner2);
sp_district = (Spinner) findViewById(R.id.spinner3);
tvCity = (TextView) findViewById(R.id.tvCity);
tvPM25 = (TextView) findViewById(R.id.tvPM25);
tvDate = (TextView) findViewById(R.id.tvDate);
// 第二个城市
ivpic21 = (ImageView) findViewById(R.id.ivpic21);
ivpic22 = (ImageView) findViewById(R.id.ivpic22);
tvweek2 = (TextView) findViewById(R.id.tvweek2);
tvwea2 = (TextView) findViewById(R.id.tvwea2);
tvwind2 = (TextView) findViewById(R.id.tvwind2);
tvtemper2 = (TextView) findViewById(R.id.tvtemper2);
// 第三个城市
ivpic31 = (ImageView) findViewById(R.id.ivpic31);
ivpic32 = (ImageView) findViewById(R.id.ivpic32);
tvweek3 = (TextView) findViewById(R.id.tvweek3);
tvwea3 = (TextView) findViewById(R.id.tvwea3);
tvwind3 = (TextView) findViewById(R.id.tvwind3);
tvtemper3 = (TextView) findViewById(R.id.tvtemper3);
// 第一个城市
ivpic11 = (ImageView) findViewById(R.id.ivpic11);
ivpic12 = (ImageView) findViewById(R.id.ivpic12);
tvweek1 = (TextView) findViewById(R.id.tvweek1);
tvwea1 = (TextView) findViewById(R.id.tvwea1);
tvwind1 = (TextView) findViewById(R.id.tvwind1);
tvtemper1 = (TextView) findViewById(R.id.tvtemper1);
// 获取到地区信息
try {
provinces = HttpUtils.getProvinces(this);
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// adapter,填充信息
provinceAdapter = new ArrayAdapter<Province>(this,
android.R.layout.simple_spinner_item, android.R.id.text1,
provinces);
provinceAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp_province.setAdapter(provinceAdapter);
cityAdapter = new ArrayAdapter<City>(this,
android.R.layout.simple_spinner_item, android.R.id.text1,
provinces.get(0).getCitys());
cityAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp_city.setAdapter(cityAdapter);
districtAdapter = new ArrayAdapter<District>(this,
android.R.layout.simple_spinner_item, android.R.id.text1,
provinces.get(0).getCitys().get(0).getDisList());
districtAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp_district.setAdapter(districtAdapter);
// 当选择省份时,城市和地方列表会变化
sp_province.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
currentPro = position;
cityAdapter = new ArrayAdapter<City>(MainActivity.this,
android.R.layout.simple_spinner_item,
android.R.id.text1, provinces.get(position).getCitys());
cityAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp_city.setAdapter(cityAdapter);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
// 当选择城市时,地方列表会变化
sp_city.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
districtAdapter = new ArrayAdapter<District>(MainActivity.this,
android.R.layout.simple_spinner_item,
android.R.id.text1, provinces.get(currentPro)
.getCitys().get(position).getDisList());
districtAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp_district.setAdapter(districtAdapter);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
// 当选择地方时,显示具体的天气情况
sp_district.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// 选择的城市
District dis = districtAdapter.getItem(position);
// Log.i("i", dis.getName());
new WeatherAsyncTask().execute(dis.getName());
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
// 异步类,获取天气数据
class WeatherAsyncTask extends AsyncTask<String, Void, Weather> {
@Override
protected Weather doInBackground(String... params) {
String url = HttpUtils.getURl(params[0]);
String jsonStr = HttpUtils.getJsonStr(url);
Weather weather = HttpUtils.fromJson(jsonStr);
Result r = weather.getResults().get(0);
/*List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
list = HttpUtils.toListMap(r);*/
for(int i = 0;i<3;i ){
Weather_data w = r.getWeather_data().get(i);
w.setDayPicture(HttpUtils.getImage(w.getDayPictureUrl()));
w.setNightPicture(HttpUtils.getImage(w.getNightPictureUrl()));
}
return weather;
}
@Override
protected void onPostExecute(Weather result ) {
Result res = result.getResults().get(0);
Weather_data wa = res.getWeather_data().get(0);
// System.out.println(res.getWeather_data());
tvCity.setText("城市:" res.getCurrentCity());
String pm2_5 = "".equals(res.getPm25()) ? "75" : res.getPm25();
// Log.i("i",res.getPm25() "wwwww");
tvPM25.setText("PM2.5:" pm2_5);
tvDate.setText("日期:" result.getDate());
// 应该为从网络上获取到的
// ivpic11.setImageResource(R.drawable.d00);
// ivpic12.setImageResource(R.drawable.d01);
ivpic11.setImageBitmap(wa.getDayPicture());
ivpic12.setImageBitmap(wa.getNightPicture());
String str = wa.getDate();
tvweek1.setText(str.substring(0, 2));
tvwea1.setText(wa.getWeather());
tvwind1.setText(wa.getWind());
tvtemper1.setText(str.substring(14, str.length()-1));
wa = res.getWeather_data().get(1);
// System.out.println(wa2);
tvtemper2.setText(wa.getTemperature());
ivpic21.setImageBitmap(wa.getDayPicture());
ivpic22.setImageBitmap(wa.getNightPicture());
tvweek2.setText(wa.getDate());
tvwea2.setText(wa.getWeather());
tvwind2.setText(wa.getWind());
tvtemper2.setText(wa.getTemperature());
wa = res.getWeather_data().get(2);
// System.out.println(wa4);
ivpic31.setImageBitmap(wa.getDayPicture());
ivpic32.setImageBitmap(wa.getNightPicture());
tvweek3.setText(wa.getDate());
tvwea3.setText(wa.getWeather());
tvwind3.setText(wa.getWind());
tvtemper3.setText(wa.getTemperature());
}
}
}
layout 代码
<LinearLayout 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:background="@drawable/bg2"
android:orientation="vertical"
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" >
<LinearLayout
android:id="@ id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Spinner
android:id="@ id/spinner1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Spinner
android:id="@ id/spinner2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_weight="1" />
<Spinner
android:id="@ id/spinner3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:id="@ id/index"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@ id/tvCity"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_weight="1" />
<TextView
android:id="@ id/tvPM25"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_weight="1" />
<TextView
android:id="@ id/tvDate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:id="@ id/lyWeather1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@ id/tvweek1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
<TextView
android:id="@ id/tvwea1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
<ImageView
android:id="@ id/ivpic11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
<ImageView
android:id="@ id/ivpic12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
<TextView
android:id="@ id/tvwind1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
<TextView
android:id="@ id/tvtemper1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
</LinearLayout>
<LinearLayout
android:id="@ id/lyWeather2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@ id/tvweek2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
<TextView
android:id="@ id/tvwea2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
<ImageView
android:id="@ id/ivpic21"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
<ImageView
android:id="@ id/ivpic22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
<TextView
android:id="@ id/tvwind2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
<TextView
android:id="@ id/tvtemper2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
</LinearLayout>
<LinearLayout
android:id="@ id/lyWeather3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@ id/tvweek3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
<TextView
android:id="@ id/tvwea3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
<ImageView
android:id="@ id/ivpic31"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
<ImageView
android:id="@ id/ivpic32"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
<TextView
android:id="@ id/tvwind3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
<TextView
android:id="@ id/tvtemper3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


支持(0) 盖楼(回复)
支持(0) 盖楼(回复)