package com.anydata.android.weatherforecast.weather.shell;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.preference.ListPreference;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.anydata.android.weatherforecast.weather.CityNotFoundException;
import com.anydata.android.weatherforecast.weather.WeatherSet;
import com.anydata.android.weatherforecast.weather.util.CityListPropertyUtil;
import com.anydata.android.weatherforecast.weather.webservice.ParseException;
import com.anydata.android.weatherforecast.weather.webservice.WeatherForecastService;
import com.anydata.android.R;
public class AddCityDialog extends Dialog {
EditText text;
Button buttonYes;
Button buttonNo;
static ListPreference parent;
public AddCityDialog(Context context, ListPreference p) {
super(context);
parent = p;
}
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.add_city_dialog);
setTitle("Add City");
text = (EditText)findViewById(R.id.add_city_name);
buttonYes = (Button) findViewById(R.id.button_yes);
buttonYes.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
String city = text.getText().toString().trim();
if(city != null && city.length() >= 1){
WeatherForecastService wsService = new WeatherForecastService();
WeatherSet ws = null;
boolean hasError = false;
try {
ws = wsService.getForecasts(city);
if(ws.getWeatherForecastConditions().size() <=0)
throw new CityNotFoundException("Sorry, can't find city: " + city);
} catch (ParseException e) {
e.printStackTrace();
hasError = true;
Toast.makeText(
v.getContext(),
"Sorry, failed to parse the message!",
Toast.LENGTH_SHORT).show();
} catch (CityNotFoundException e){
e.printStackTrace();
hasError = true;
Toast.makeText(
v.getContext(),
e.getMessage(),
Toast.LENGTH_SHORT).show();
} catch (Exception e){
e.printStackTrace();
hasError = true;
Toast.makeText(
v.getContext(),
"Sorry, exception happed when adding city: " + city,
Toast.LENGTH_SHORT).show();
}
if(hasError == false){
CityListPropertyUtil.addCity(parent, city);
Toast.makeText(
v.getContext(),
"Add city: " + city + " successfully",
Toast.LENGTH_SHORT).show();
}
dismiss();
}
}
});
buttonNo = (Button) findViewById(R.id.button_no);
buttonNo.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
dismiss();
}
});
}
//called when this dialog is dismissed
protected void onStop() {
Log.d("TAG","+++++++++++++++++++++++++++");
}
}
|