Android Open Source - android-weather-demo-application Forecast Fragment






From Project

Back to project page android-weather-demo-application.

License

The source code is released under:

GNU General Public License

If you think the Android project android-weather-demo-application listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.openweathermap.fragment;
/*  w w w  .j av  a 2 s . c  o m*/
import org.openweathermap.activity.MainActivity;
import org.openweathermap.sql.model.CityModel;
import org.openweathermap.sql.model.WeatherModel;
import org.openweathermap.utils.RESTProvider;
import org.openweathermap.utils.StringHelper;
import org.openweathermap.view.WeatherDataRowView;
import org.openweathermap.volley.provider.VolleyImageLoader;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.ink.weather.R;

public class ForecastFragment extends Fragment {
  private TextView uiCityTextView;
  private TextView uiLongLatTextView;
  private TextView uiWeatherConditionMainTextView;
  private TextView uiWeatherConditionDescriptionTextView;
  private TextView uiTemperatureTextView;
  private NetworkImageView uiNetworkImageView;
  private WeatherDataRowView uiTemperatureRangeWeatherDataRowView;
  private WeatherDataRowView uiAtmosphericPressureWeatherDataRowView;
  private WeatherDataRowView uiHumidityRangeWeatherDataRowView;
  
  private int mDataPosition;
  
  public static final String ARG_DATA_POSITION = "ARG_DATA_POSITION";
  
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_forecast, container, false);
    
    uiCityTextView = (TextView)view.findViewById(R.id.fragment_forecast_city);
    uiLongLatTextView = (TextView)view.findViewById(R.id.fragment_forecast_lng_lat);
    uiWeatherConditionMainTextView = (TextView)view.findViewById(R.id.fragment_forecast_weather_conditions_main);
    uiWeatherConditionDescriptionTextView = (TextView)view.findViewById(R.id.fragment_forecast_weather_conditions_description);
    uiTemperatureTextView = (TextView)view.findViewById(R.id.fragment_forecast_temperature);
    uiNetworkImageView = (NetworkImageView)view.findViewById(R.id.fragment_forecast_weather_conditions_icon);
    uiTemperatureRangeWeatherDataRowView = (WeatherDataRowView)view.findViewById(R.id.fragment_forecast_temperature_range_row);
    uiAtmosphericPressureWeatherDataRowView = (WeatherDataRowView)view.findViewById(R.id.fragment_forecast_atmospheric_pressure_row);
    uiHumidityRangeWeatherDataRowView = (WeatherDataRowView)view.findViewById(R.id.fragment_forecast_humidity_row);
    
    return view;
  }
  
  @Override
  public void onResume() {
    super.onResume();
    mDataPosition = getArguments().getInt(ARG_DATA_POSITION);
    
    WeatherModel weatherModel = ((MainActivity)getActivity()).getWeatherModelArray()[mDataPosition];
    CityModel cityModel = ((MainActivity)getActivity()).getCityModel();
    init(weatherModel,cityModel);
  }
  
//  @Override
//  public void onActivityCreated(Bundle savedInstanceState) {
//    super.onActivityCreated(savedInstanceState);
//    mDataPosition = getArguments().getInt(ARG_DATA_POSITION);
//    
//    WeatherModel weatherModel = ((MainActivity)getActivity()).getWeatherModelArray()[mDataPosition];
//    CityModel cityModel = ((MainActivity)getActivity()).getCityModel();
//    init(weatherModel,cityModel);
//    //requestWeatherConditionIcon(dayDTO.getWeatherList()[0].getIcon());
//  }
  
  /**
   * Populate the view with city data and fade the layout in
   */
  public void init(WeatherModel weatherModel, CityModel cityModel) {
    String locationName = cityModel.getName() + " (" + cityModel.getCountry() + ")";
    String latitude = cityModel.getLatitude();
    String longitude = cityModel.getLongitude();
    String coords = "(" + latitude + "," + longitude + ")";
    
    String mainCondition = weatherModel.getDescription();
    String conditionDescription = "(" + weatherModel.getDescriptionDetailed() + ")";
    
    String temperature = String.valueOf(weatherModel.getTmpDay()) + StringHelper.buildDegreeSymbol();
    double temperatureMin = weatherModel.getTmpMin();
    double temperatureMax = weatherModel.getTmpMax();
    String temperatureRange = String.valueOf(temperatureMin) + " - " + String.valueOf(temperatureMax) + StringHelper.buildDegreeSymbol();
    String humidity = String.valueOf(weatherModel.getHumidity()) + "%";
    String atmosphericPressure = String.valueOf(weatherModel.getPressure()) + "hpa";
    
    uiCityTextView.setText(locationName);
    uiLongLatTextView.setText(coords);
    uiWeatherConditionMainTextView.setText(mainCondition);
    uiWeatherConditionDescriptionTextView.setText(conditionDescription);
    uiTemperatureTextView.setText(temperature);
    
    uiTemperatureRangeWeatherDataRowView.setTitle(getResources().getString(R.string.view_weather_data_row_temperature_range));
    uiTemperatureRangeWeatherDataRowView.setValue(temperatureRange);
    
    uiAtmosphericPressureWeatherDataRowView.setTitle(getResources().getString(R.string.view_weather_data_row_atmospheric_pressure));
    uiAtmosphericPressureWeatherDataRowView.setValue(atmosphericPressure);
    
    uiHumidityRangeWeatherDataRowView.setTitle(getResources().getString(R.string.view_weather_data_row_humidity));
    uiHumidityRangeWeatherDataRowView.setValue(humidity);
  }
  
  /**
   * Download the weather condition icon
   * @param  iconCode  The weather condition icon to download
   */
  private void requestWeatherConditionIcon(String iconCode) {
    String iconUrl = RESTProvider.getWeatherConditionIcon(iconCode, getActivity());
    VolleyImageLoader volleyImageLoader = new VolleyImageLoader(iconUrl);
    
    uiNetworkImageView.setImageUrl(
      volleyImageLoader.getUrl(),
      volleyImageLoader.getImageLoader()
    );
  }
}




Java Source Code List

org.openweathermap.WeatherApplication.java
org.openweathermap.activity.MainActivity.java
org.openweathermap.activity.base.BaseActivity.java
org.openweathermap.adapter.CityAdapter.java
org.openweathermap.adapter.ForecastAdapter.java
org.openweathermap.asynctask.GetCitiesAsyncTask.java
org.openweathermap.asynctask.GetLastCityAsyncTask.java
org.openweathermap.asynctask.SaveForecastAsyncTask.java
org.openweathermap.asynctask.SearchCitiesAsyncTask.java
org.openweathermap.asynctask.base.BaseAsyncTask.java
org.openweathermap.asynctask.base.BaseResponse.java
org.openweathermap.asynctask.base.IParam.java
org.openweathermap.asynctask.param.SaveForecastParam.java
org.openweathermap.asynctask.param.SearchCitiesParam.java
org.openweathermap.asynctask.response.GetCitiesResponse.java
org.openweathermap.asynctask.response.GetLastCityResponse.java
org.openweathermap.asynctask.response.SaveForecastResponse.java
org.openweathermap.dto.CityDTO.java
org.openweathermap.dto.CoordDTO.java
org.openweathermap.dto.DayDTO.java
org.openweathermap.dto.ResultDTO.java
org.openweathermap.dto.TempDTO.java
org.openweathermap.dto.WeatherDTO.java
org.openweathermap.dto.base.IDTO.java
org.openweathermap.fragment.ForecastFragment.java
org.openweathermap.fragment.ModalDialogFragment.java
org.openweathermap.fragment.SelectCountryFragment.java
org.openweathermap.sql.SQLInitProvider.java
org.openweathermap.sql.model.CityModel.java
org.openweathermap.sql.model.IconModel.java
org.openweathermap.sql.model.WeatherModel.java
org.openweathermap.utils.DateHelper.java
org.openweathermap.utils.RESTProvider.java
org.openweathermap.utils.ReflectionHelper.java
org.openweathermap.utils.ResourceHelper.java
org.openweathermap.utils.StringHelper.java
org.openweathermap.view.CityListItemView.java
org.openweathermap.view.DateDisplayView.java
org.openweathermap.view.ViewPagerFade.java
org.openweathermap.view.ViewPagerIndicatorView.java
org.openweathermap.view.WeatherDataRowView.java
org.openweathermap.volley.callback.VolleyResponseCallback.java
org.openweathermap.volley.provider.VolleyImageLoader.java
org.openweathermap.volley.provider.VolleyRequest.java