package com.example.myapplication;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import com.google.gson.Gson;
import java.io.IOException;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Spinner mSpinner; // 下拉框对象
private String[] mCities,mcitiesid; // 城市及对应id
private ArrayAdapter<String> mspAdapter; // 适配器组件 绑定下拉框控件
private TextView tempTv,cityTv,conditionTv,dateTv,windTv,tenpRangerTv,air_index,chuxing_index;
private TextView kouzhao_index,sport_index,kaichuang_index,kongtiao_index;
private ImageView dayTv;
private String air_index_info,sport_index_info,chuxing_index_info,kouzhao_index_info,kaichuang_index_info,kongtiao_index_info;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView_NSL();
}
//初始化
private void initView_NSL(){
mSpinner = findViewById(R.id.sp_city);
mCities = getResources().getStringArray(R.array.cities);
mcitiesid = getResources().getStringArray(R.array.citiesid);
mspAdapter = new ArrayAdapter<>(this,R.layout.sp_item_layout,mCities); // 数组适配器
mSpinner.setAdapter(mspAdapter);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { // 给下拉框注册事件监听
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String selectedCity = mCities[i]; // 获取下拉框中所选城市
String selectedCityid = mcitiesid[i]; // 获取下拉框中所选城市的id
Log.d("NSL","------weather----NSL---" + selectedCity);
Log.d("NSL","------weather----NSL---" + selectedCityid);
getWeatherOfCity(selectedCityid);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
tempTv = findViewById(R.id.frag_tv_currenttemp);
cityTv = findViewById(R.id.frag_tv_city);
conditionTv = findViewById(R.id.frag_tv_condition);
dateTv = findViewById(R.id.frag_tv_date);
windTv = findViewById(R.id.frag_tv_wind);
tenpRangerTv = findViewById(R.id.frag_tv_temprange);
air_index = findViewById(R.id.frag_index_air);
air_index.setOnClickListener(this);
chuxing_index = findViewById(R.id.frag_index_chuxing);
chuxing_index.setOnClickListener(this);
kouzhao_index = findViewById(R.id.frag_index_kouzhao);
kouzhao_index.setOnClickListener(this);
sport_index = findViewById(R.id.frag_index_yundong);
sport_index.setOnClickListener(this);
kaichuang_index = findViewById(R.id.frag_index_kaichuang);
kaichuang_index.setOnClickListener(this);
kongtiao_index = findViewById(R.id.frag_index_kongtiao);
kongtiao_index.setOnClickListener(this);
dayTv = findViewById(R.id.frag_tv_today);
}
private void getWeatherOfCity(final String selectdCityid){
new Thread(new Runnable() {
String weatherOfCity = "";
@Override
public void run() {
try {
weatherOfCity = NetUtil_ChenXuan.getWeather(selectdCityid);
} catch (IOException e) {
e.printStackTrace();
}
//将数据传递给主线程
Message message = Message.obtain(); // 获取当前分线程的信息
message.what = 0; // 消息代码,便于区分
message.obj = weatherOfCity;
mHandler.sendMessage(message); // 发送信息
}
}).start();
}
//使用Handler来接受和发送信息
private Handler mHandler=new Handler(Looper.myLooper()){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
if (msg.what == 0){
String weather=(String)msg.obj;
Log.d("NSL","---------主线程收到天气信息---------" + weather);
Gson gson = new Gson();
WeatherBean weatherBean = gson.fromJson(weather,WeatherBean.class);//将天气数据由JSON格式转换为WeatherBean对象
if (weatherBean!=null){
updateUiOfWeather(weatherBean);
}
}
}
};
//对UI界面数据进行更新
private void updateUiOfWeather(WeatherBean weatherBean) {
String tem = weatherBean.getTem();
String tem1= weatherBean.getTem1();
String tem2 = weatherBean.getTem2();
String city = weatherBean.getCity();
String wea = weatherBean.getWea();
String wea_img = weatherBean.getWea_img();
String date = weatherBean.getDate();
String win = weatherBean.getWin();
String win_speed= weatherBean.getWin_speed();
ApiBean aqi = weatherBean.getAqi();
air_index_info = aqi.getAir_tips();
chuxing_index_info = aqi.getWaichu();
kouzhao_index_info = aqi.getKouzhao();
sport_index_info = aqi.getYundong();
kaichuang_index_info = aqi.getKaichuang();
kongtiao_index_info = aqi.getJinghuaqi();
tempTv.setText(tem + "℃");
cityTv.setText(city);
conditionTv.setText(wea);
dateTv.setText(date);
windTv.setText(win + win_speed);
tenpRangerTv.setText(tem2 + "~" + tem1 + "℃");
dayTv.setImageResource(getImageOfWeather(wea_img));
}
private int getImageOfWeather(String wea_img){
int result=0;
//xue、lei、shachen、wu、bingbao、yun、yu、yin、qing
switch (wea_img){
case "qing":
result = R.mipmap.biz_plugin_weather_qing;
break;
case "yin":
result = R.mipmap.biz_plugin_weather_yin;
break;
case "wu":
result = R.mipmap.biz_plugin_weather_wu;
break;
case "yu":
result = R.mipmap.biz_plugin_weather_xiaoyu;
break;
case "yun":
result = R.mipmap.biz_plugin_weather_duoyun;
break;
case "bingbao":
result = R.mipmap.biz_plugin_weather_zhongxue;
break;
case "shachen":
result = R.mipmap.biz_plugin_weather_shachenbao;
break;
case "lei":
result = R.mipmap.biz_plugin_weather_leizhenyu;
break;
case "xue":
result = R.mipmap.biz_plugin_weather_xiaoxue;
break;
}
return result;
}
public void onClick(View view) {//点击事件处理器
Log.d("NSL","----------事件处理---------");
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
switch (view.getId()){
case R.id.frag_index_air:
builder.setTitle("空气指数");
builder.setMessage(air_index_info);
builder.setPositiveButton("确定",null);
break;
case R.id.frag_index_chuxing:
builder.setTitle("出行指数");
builder.setMessage(chuxing_index_info);
builder.setPositiveButton("确定",null);
break;
case R
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
weather-forecast-app-master.zip (78个子文件)
weather-forecast-app-master
gradle.properties 1KB
gradle
wrapper
gradle-wrapper.jar 58KB
gradle-wrapper.properties 232B
PreviewPicture
Snipaste_1.jpg 52KB
Snipaste_2.jpg 53KB
app
src
androidTest
java
com
example
myapplication
ExampleInstrumentedTest.java 764B
test
java
com
example
myapplication
ExampleUnitTest.java 386B
main
java
com
example
myapplication
NetUtil_ChenXuan.java 3KB
WeatherBean.java 2KB
ApiBean.java 2KB
MainActivity.java 9KB
res
mipmap-xxhdpi
ic_launcher_round.webp 6KB
ic_launcher.webp 3KB
mipmap-hdpi
biz_plugin_weather_qing.png 3KB
icon_cloth.png 2KB
biz_plugin_weather_yin.png 4KB
biz_plugin_weather_leizhenyu.png 4KB
biz_plugin_weather_zhenyu.png 4KB
biz_plugin_weather_duoyun.png 3KB
biz_plugin_weather_xiaoxue.png 3KB
background_nsl.png 537KB
biz_plugin_weather_zhongyu.png 4KB
biz_plugin_weather_daxue.png 4KB
icon_car.png 2KB
icon_air.png 1KB
biz_plugin_weather_dabaoyu.png 4KB
icon_light.png 2KB
biz_plugin_weather_wu.png 3KB
biz_plugin_weather_dayu.png 4KB
biz_plugin_weather_zhongxue.png 4KB
biz_plugin_weather_xiaoyu.png 4KB
icon_ganmao.png 3KB
ic_launcher_round.webp 3KB
biz_plugin_weather_zhenxue.png 5KB
biz_plugin_weather_shachenbao.png 3KB
biz_plugin_weather_tedabaoyu.png 4KB
icon_sport.png 3KB
biz_plugin_weather_yujiaxue.png 4KB
biz_plugin_weather_baoyu.png 4KB
biz_plugin_weather_baoxue.png 4KB
icon.png 98KB
ic_launcher.webp 1KB
biz_plugin_weather_leizhenyubingbao.png 4KB
drawable-v24
ic_launcher_foreground.xml 2KB
mipmap-anydpi-v26
ic_launcher.xml 272B
ic_launcher_round.xml 272B
values-night
themes.xml 835B
mipmap-mdpi
ic_launcher_round.webp 2KB
ic_launcher.webp 982B
mipmap-xxxhdpi
ic_launcher_round.webp 8KB
ic_launcher.webp 4KB
mipmap-xhdpi
ic_launcher_round.webp 4KB
ic_launcher.webp 2KB
xml
data_extraction_rules.xml 551B
backup_rules.xml 478B
values
colors.xml 378B
strings.xml 698B
themes.xml 2KB
layout
sp_item_layout.xml 313B
activity_main.xml 7KB
drawable
ic_launcher_background.xml 5KB
AndroidManifest.xml 1KB
proguard-rules.pro 750B
libs
gson-2.8.5.jar 236KB
build.gradle 1KB
.gitignore 6B
LICENSE 1KB
gradlew.bat 3KB
build.gradle 292B
.idea
.name 14B
vcs.xml 180B
misc.xml 1KB
compiler.xml 169B
gradle.xml 834B
.gitignore 47B
settings.gradle 330B
gradlew 6KB
.gitignore 225B
共 78 条
- 1
资源评论
程序员柳
- 粉丝: 6273
- 资源: 1295
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功