package com.itxinke.www;
import java.util.LinkedList;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
public class WeatherActivity extends Activity
{
private static final String TAG = "WEATHERACTIVITY";
private static final int SETTING = 0;
private static final int BACK = 1;
private int chicked_city;
private LinkedList<WeatherInfo> weatherInfos;
private ImageView current_image;
private ImageView weatherImageFirst,weatherImageSecond,weatherImageThird,weatherImageForth;
private TextView current_weather;
private TextView dateInfoFirst,dateInfoSecond,dateInfoThird,dateInfoForth;
private TextView tepInfoFirst,tepInfoSecond,tepInfoThird,tepInfoForth;
private ImageButton refreshButton;
private GetWeather weather;
private String[] city;
private String[] cityCn;
private String[] weekChina =
{
"星期一","星期二","星期三","星期四","星期五","星期六","星期日"
};
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// TODO Auto-generated method stub
//return super.onCreateOptionsMenu(menu);
menu.add(0, SETTING, SETTING, R.string.setting);
menu.add(0, BACK, BACK, R.string.back);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// TODO Auto-generated method stub
//return super.onOptionsItemSelected(item);
int item_id = item.getItemId();
switch(item_id)
{
case SETTING:
/*new AlertDialog.Builder(this).setTitle(WeatherActivity.this.getText(R.string.choice))
.setSingleChoiceItems(city, chicked_city, new DialogOnClickListener()).show();*/
Builder builder = new AlertDialog.Builder(this);
builder.setTitle(WeatherActivity.this.getText(R.string.choice));
builder.setSingleChoiceItems(((WeatherApplication)getApplication()).getStringArrayCn(), chicked_city, new DialogOnClickListener());
builder.setPositiveButton(getText(R.string.ok), new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
// TODO Auto-generated method stub
Log.v(TAG, "which = " + which);
if(chicked_city != -1)
{
//SharedPreferences state = getPreferences(Activity.MODE_WORLD_READABLE);
SharedPreferences state = getSharedPreferences("data", MODE_WORLD_READABLE | MODE_WORLD_WRITEABLE);
SharedPreferences.Editor editor = state.edit();
editor.putInt("chicked_city", chicked_city);
editor.commit();
weather.reGetWeatherToApplication(city[chicked_city]);
chicked_city = -1;
showAllViews();
}
}
});
builder.setNegativeButton(getText(R.string.cancel), new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
// TODO Auto-generated method stub
chicked_city= -1;
}
});
Dialog dialog= builder.create();
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
dialog.show();
break;
case BACK:
this.finish();
break;
}
return true;
}
class DialogOnClickListener implements DialogInterface.OnClickListener
{
@Override
public void onClick(DialogInterface dialog, int which)
{
// TODO Auto-generated method stub
Log.v(TAG, "chicked_city = " + which);
chicked_city = which;
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//设置全屏显示
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
weather = new GetWeather(this);
city = ((WeatherApplication)getApplication()).getStringArray();
cityCn = ((WeatherApplication)getApplication()).getStringArrayCn();
findAllViewsId();
showAllViews();
printAllInfos();
// button = (Button)findViewById(R.id.button);
// button.setOnClickListener(new Button.OnClickListener()
}
private void findAllViewsId()
{
current_image = (ImageView)findViewById(R.id.current_image);
current_weather = (TextView)findViewById(R.id.current_weather);
weatherImageFirst = (ImageView)findViewById(R.id.weatherImageFirst);
weatherImageSecond = (ImageView)findViewById(R.id.weatherImageSecond);
weatherImageThird = (ImageView)findViewById(R.id.weatherImageThird);
weatherImageForth = (ImageView)findViewById(R.id.weatherImageForth);
dateInfoFirst = (TextView)findViewById(R.id.dateInfoFirst);
dateInfoSecond = (TextView)findViewById(R.id.dateInfoSecond);
dateInfoThird = (TextView)findViewById(R.id.dateInfoThird);
dateInfoForth = (TextView)findViewById(R.id.dateInfoForth);
tepInfoFirst = (TextView)findViewById(R.id.tepInfoFirst);
tepInfoSecond = (TextView)findViewById(R.id.tepInfoSecond);
tepInfoThird = (TextView)findViewById(R.id.tepInfoThird);
tepInfoForth = (TextView)findViewById(R.id.tepInfoForth);
refreshButton = (ImageButton)findViewById(R.id.refresh_button);
}
private void showAllViews()
{
weatherInfos = ((WeatherApplication)getApplication()).getWeatherInfos();
WeatherInfo weatherInfo = weatherInfos.getFirst();
current_image.setBackgroundResource(getImageId(weatherInfo));
String current = "实时天气";
CharSequence csHu = getText(R.string.humidity);
CharSequence csWin = getText(R.string.wind);
CharSequence mph = getText(R.string.mph);
SharedPreferences state = getSharedPreferences("data", MODE_WORLD_READABLE | MODE_WORLD_WRITEABLE);
int index = state.getInt("chicked_city", 0);
current =cityCn[index] + "\n\n\n\n" + current + "\n" + csHu + ":" + (weatherInfo.getHumidity().split(" "))[1] + "\n" + csWin + ":" +
getText(getDerectionId(weatherInfo)) + "\n" + getText(R.string.speed) + ":" +
(weatherInfo.getWind_condition().split(" "))[3] + mph;
current_weather.setText(current);
Log.v(TAG, weatherInfo.toString());
weatherInfo = weatherInfos.get(1);
dateInfoFirst.setText(getWeekChina(weatherInfo));
weatherImageFirst.setBackgroundResource(getImageSmallId(weatherInfo));
tepInfoFirst.setText(toC(weatherInfo.getLow()) + "℃\n" + toC(weatherInfo.getHigh()) + "℃\n");
weatherInfo = weatherInfos.get(2);
dateInfoSecond.setText(getWeekChina(weatherInfo));
weatherImageSecond.setBackgroundResource(getImageSmallId(weatherInfo));
tepInfoSecond.setText(toC(weatherInfo.getLow()) + "℃\n" + toC(weatherInfo.getHigh()) + "℃\n");
weatherInfo = weatherInfos.get(3);
dateInfoThird.setText(getWeekChina(weatherInfo));
weatherImageThird.setBackgroundResource(getImageSmallId(weatherInfo));
tepInfoThird.setText(toC(weatherInfo.getLow()) + "℃\n" + toC(weatherInfo.getHigh()) + "℃\n");
weatherInfo = weatherInfos.get(4);
dateInfoForth.setText(getWeekChina(weatherInfo));
weatherImageForth.setBackgroundResource(getImageSmallId(weatherInfo));
tepInfoForth.setText(toC(weatherInfo.getLow()) + "℃\n" + toC(weatherInfo.getHigh()) + "℃\n");
refreshButton.setOnClickListener(new RefreshButtonListener());
}
class RefreshButtonListener implements ImageButton.OnClickListener
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
SharedPreferences state = getPreferences(Activity.MODE_WORLD_READABLE);
int index = state.get