package cc.icoc.javaxu.weather;
import java.io.IOException;
import java.util.List;
import org.xmlpull.v1.XmlPullParserException;
import cc.icoc.javaxu.weather.service.WeatherBean;
import cc.icoc.javaxu.weather.service.WebFunction;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class WeatherActivity extends Activity {
List<WeatherBean> weatherBeans;
String s,tip,details;
int[] imageArray = new int[7];
WebFunction function = new WebFunction();
String theCityName;
Button toIntroduce;
TextView cityName,weather,weatherState,temp,wind,pubTime,tomorrow,third;
ImageView weatherPicture,todayPicture1,todayPicture2,tomorrowPicture1,tomorrowPicture2,
thirdPicture1,thirdPicture2,weatherPicture2;
TextView todayTemp,todayWind,tomorrowTemp,tomorrowWind,tomorrowWeather,thirdTemp,thirdWind,thirdWeather;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.weather);
System.out.println("Create");
//读取共享偏好设置内的城市名,如果没有则调用showAlert方法提醒用户输入一个城市名并保存到共享偏好文件里
SharedPreferences sp = WeatherActivity.this.getSharedPreferences("myfile", MODE_PRIVATE);
//入伙theCityName的值为空则取设定的默认值“0”返回
if(sp.getString("theCityName", "0") == "0")
{
showAlert();
Toast.makeText(WeatherActivity.this, "请先设置城市", 1).show();
}
else
{
theCityName = sp.getString("theCityName", "0");
initView();
initInformation(theCityName);//给控件填充天气信息
}
}
public void initView()
{
System.out.println("initView");
tomorrow = (TextView)this.findViewById(R.id.mingtian);
third = (TextView)this.findViewById(R.id.houtian);
temp = (TextView)this.findViewById(R.id.temp);
cityName = (TextView)this.findViewById(R.id.city_name);
wind = (TextView)this.findViewById(R.id.wind);
pubTime = (TextView)this.findViewById(R.id.pubTime);
toIntroduce = (Button)this.findViewById(R.id.introduce);
Click click = new Click();
toIntroduce.setOnClickListener(click);
weather = (TextView)this.findViewById(R.id.weather);
weatherState = (TextView)this.findViewById(R.id.weather_state);
weatherPicture2 = (ImageView)this.findViewById(R.id.weatherBigPicture2);
weatherPicture = (ImageView)this.findViewById(R.id.weatherBigPicture);
tomorrowPicture1 = (ImageView)this.findViewById(R.id.tomorrow_picture1);
tomorrowPicture2 = (ImageView)this.findViewById(R.id.tomorrow_picture2);
thirdPicture1 = (ImageView)this.findViewById(R.id.third_picture1);
thirdPicture2 = (ImageView)this.findViewById(R.id.third_picture2);
tomorrowTemp = (TextView)this.findViewById(R.id.tomorrow_temp);
tomorrowWind = (TextView)this.findViewById(R.id.tomorrow_wind);
tomorrowWeather = (TextView)this.findViewById(R.id.tomorrow_weather);
thirdTemp = (TextView)this.findViewById(R.id.third_temp);
thirdWind = (TextView)this.findViewById(R.id.third_wind);
thirdWeather = (TextView)this.findViewById(R.id.third_weather);
}
public void initInformation(String city) {
System.out.println("initFormation");
try {
weatherBeans = function.getWeather(city);
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
if(weatherBeans != null)
{
for(WeatherBean bean:weatherBeans)
{
if(bean.getReturnResult().equals("null"))
{
Toast.makeText(WeatherActivity.this, "查询结果为空!", 1).show();
weatherState.setText("抱歉该城市或区域暂时不被支持,请更换一个城市");
tip = "";
details = "";
cityName.setText("");
temp.setText("");
wind.setText("");
weather.setText("");
pubTime.setText("");
weatherPicture2.setImageResource(View.GONE);
weatherPicture.setImageResource(View.GONE);
tomorrowPicture1.setImageResource(View.GONE);
tomorrowPicture2.setImageResource(View.GONE);
thirdPicture1.setImageResource(View.GONE);
thirdPicture2.setImageResource(View.GONE);
tomorrowTemp.setText("");
tomorrowWind.setText("");
tomorrowWeather.setText("");
thirdTemp.setText("");
thirdWind.setText("");
thirdWeather.setText("");
tomorrow.setText("");
third.setText("");
}
else
{
tip = bean.getTip();
details = bean.getIntroduce();
cityName.setText(bean.getCity());
temp.setText(bean.getTemperature());
wind.setText("风力风向:\t"+bean.getWind());
weather.setText(bean.getWeather());
pubTime.setText("最后更新:"+bean.getTime());
weatherState.setText(bean.getState());
weatherPicture2.setImageResource(parseIcon(bean.getWeather_picture2()));
weatherPicture.setImageResource(parseIcon(bean.getWeather_picture()));
tomorrowPicture1.setImageResource(parseIcon(bean.getTomorrow__picture1()));
tomorrowPicture2.setImageResource(parseIcon(bean.getTomorrow__picture2()));
thirdPicture1.setImageResource(parseIcon(bean.getThird__picture1()));
thirdPicture2.setImageResource(parseIcon(bean.getThird__picture2()));
tomorrowTemp.setText(bean.getTomorrow_temp());
tomorrowWind.setText(bean.getTomorrow_wind());
tomorrowWeather.setText(bean.getTomorrow_weather());
thirdTemp.setText(bean.getThird_temp());
thirdWind.setText(bean.getThird_wind());
thirdWeather.setText(bean.getThird_weather());
tomorrow.setText("明天");
third.setText("后天");
}
}
}
else
{
System.out.println("接收到空值");
}
}
public int parseIcon(String strIcon){
if(strIcon == null){
return -1;
}
if ("0.gif".equals(strIcon))
return R.drawable.a_0;
if ("1.gif".equals(strIcon))
return R.drawable.a_1;
if ("2.gif".equals(strIcon))
return R.drawable.a_2;
if ("3.gif".equals(strIcon))
return R.drawable.a_3;
if ("4.gif".equals(strIcon))
return R.drawable.a_4;
if ("5.gif".equals(strIcon))
return R.drawable.a_5;
if ("6.gif".equals(strIcon))
return R.drawable.a_6;
if ("7.gif".equals(strIcon))
return R.drawable.a_7;
if ("8.gif".equals(strIcon))
return R.drawable.a_8;
if ("9.gif".equals(strIcon))
return R.drawable.a_9;
if ("10.gif".equals(strIcon))
return R.drawable.a_10;
if ("11.gif".equals(strIcon))
return R.drawable.a_11;
if ("12.gif".equals(strIcon))
return R.drawable.a_12;
if ("13.gif".equals(strIcon))
return R.drawable.a_13;
if ("14.gif".equals(strIcon))
return R.drawable.a_14;
if ("15.gif".equals(strIcon))
return R.drawable.a_15;
if ("16.gif".equals(strIcon))
return R.drawable.a_16;
if ("17.gif".equals(strIcon))
return R.drawable.a_17;
if ("18.gif".equals(strIcon))
return R.drawable.a_18;
if ("19.gif".equals(strIcon))
return R.drawable.a_19;
if ("20.gif".equals(strIcon))
return R.drawable.a_20;
if ("21.gif".equals(strIcon))
return R.drawable.a_21;
if ("22.gif".equals(strIcon))
return R.drawable.a_22;
if ("23.gif".equals(strIcon))
return R.drawable.a_23;
if ("24.gif".equals(strIcon))
return R.drawable.a_24;
if ("25.gif".equals(strIcon))
return R.drawable.a_25;
if ("26.gif".equals(strIcon))
return R.drawable.a_26;
if ("27.gif".equals(strIcon))
return R.drawable.a_27;
if ("28.gif".equals(strIcon))
return R.drawable.a_28;
if ("29.gif".equal