Exemple #1
0
  public static String getFormattedWind(Context context, float windSpeed, float degrees) {
    int windFormat;
    if (Utility.isMetric(context)) {
      windFormat = R.string.format_wind_kmh;
    } else {
      windFormat = R.string.format_wind_mph;
      windSpeed = .621371192237334f * windSpeed;
    }

    // From wind direction in degrees, determine compass direction as a string (e.g NW)
    // You know what's fun, writing really long if/else statements with tons of possible
    // conditions.  Seriously, try it!
    String direction = "Unknown";
    if (degrees >= 337.5 || degrees < 22.5) {
      direction = "N";
    } else if (degrees >= 22.5 && degrees < 67.5) {
      direction = "NE";
    } else if (degrees >= 67.5 && degrees < 112.5) {
      direction = "E";
    } else if (degrees >= 112.5 && degrees < 157.5) {
      direction = "SE";
    } else if (degrees >= 157.5 && degrees < 202.5) {
      direction = "S";
    } else if (degrees >= 202.5 && degrees < 247.5) {
      direction = "SW";
    } else if (degrees >= 247.5 && degrees < 292.5) {
      direction = "W";
    } else if (degrees >= 292.5 && degrees < 337.5) {
      direction = "NW";
    }
    return String.format(context.getString(windFormat), windSpeed, direction);
  }
  @Override
  public void bindView(View view, Context context, Cursor cursor) {

    ViewHolder viewHolder = (ViewHolder) view.getTag();

    int viewType = getItemViewType(cursor.getPosition());
    int weatherId = cursor.getInt(ForecastFragment.COL_WEATHER_CONDITION_ID);
    int fallbackIconId;
    switch (viewType) {
      case VIEW_TYPE_TODAY:
        {
          // Get weather icon
          fallbackIconId = Utility.getArtResourceForWeatherCondition(weatherId);
          break;
        }
      default:
        {
          // Get weather icon
          fallbackIconId = Utility.getIconResourceForWeatherCondition(weatherId);
          break;
        }
    }

    Glide.with(mContext)
        .load(Utility.getArtUrlForWeatherCondition(mContext, weatherId))
        .error(fallbackIconId)
        .crossFade()
        .into(viewHolder.iconView);

    // Read date from cursor
    long dateInMillis = cursor.getLong(ForecastFragment.COL_WEATHER_DATE);
    // Find TextView and set formatted date on it
    viewHolder.dateView.setText(Utility.getFriendlyDayString(context, dateInMillis));

    // Read weather forecast from cursor
    String description = cursor.getString(ForecastFragment.COL_WEATHER_DESC);
    // Find TextView and set weather forecast on it
    viewHolder.descriptionView.setText(description);

    // For accessibility, add a content description to the icon field
    viewHolder.iconView.setContentDescription(description);
    viewHolder.descriptionView.setContentDescription(
        context.getString(R.string.a11y_forecast, description));

    // Read user preference for metric or imperial temperature units
    boolean isMetric = Utility.isMetric(context);

    // Read high temperature from cursor
    String high =
        Utility.formatTemperature(context, cursor.getDouble(ForecastFragment.COL_WEATHER_MAX_TEMP));
    viewHolder.highTempView.setText(high);
    viewHolder.highTempView.setContentDescription(context.getString(R.string.a11y_high_temp, high));

    // Read low temperature from cursor
    String low =
        Utility.formatTemperature(context, cursor.getDouble(ForecastFragment.COL_WEATHER_MIN_TEMP));
    viewHolder.lowTempView.setText(low);
    viewHolder.lowTempView.setContentDescription(context.getString(R.string.a11y_low_temp, low));
  }
  @Override
  public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    if (data != null && data.moveToFirst()) {
      // Read weather condition ID from cursor
      int weatherId = data.getInt(COL_WEATHER_CONDITION_ID);

      // Use weather art image
      mIconView.setImageResource(Utility.getArtResourceForWeatherCondition(weatherId));

      // Read date from cursor and update views for day of week and date
      long date = data.getLong(COL_WEATHER_DATE);
      String friendlyDateText = Utility.getDayName(getActivity(), date);
      String dateText = Utility.getFormattedMonthDay(getActivity(), date);
      mFriendlyDateView.setText(friendlyDateText);
      mDateView.setText(dateText);

      // Read description from cursor and update view
      String description = data.getString(COL_WEATHER_DESC);
      mDescriptionView.setText(description);

      // Read high temperature from cursor and update view
      boolean isMetric = Utility.isMetric(getActivity());

      double high = data.getDouble(COL_WEATHER_MAX_TEMP);
      String highString = Utility.formatTemperature(getActivity(), high);
      mHighTempView.setText(highString);

      // Read low temperature from cursor and update view
      double low = data.getDouble(COL_WEATHER_MIN_TEMP);
      String lowString = Utility.formatTemperature(getActivity(), low);
      mLowTempView.setText(lowString);

      // Read humidity from cursor and update view
      float humidity = data.getFloat(COL_WEATHER_HUMIDITY);
      mHumidityView.setText(getActivity().getString(R.string.format_humidity, humidity));

      // Read wind speed and direction from cursor and update view
      float windSpeedStr = data.getFloat(COL_WEATHER_WIND_SPEED);
      float windDirStr = data.getFloat(COL_WEATHER_DEGREES);
      mWindView.setText(Utility.getFormattedWind(getActivity(), windSpeedStr, windDirStr));

      // Read pressure from cursor and update view
      float pressure = data.getFloat(COL_WEATHER_PRESSURE);
      mPressureView.setText(getActivity().getString(R.string.format_pressure, pressure));

      // We still need this for the share intent
      mForecast = String.format("%s - %s - %s/%s", dateText, description, high, low);

      // If onCreateOptionsMenu has already happened, we need to update the share intent now.
      if (mShareActionProvider != null) {
        mShareActionProvider.setShareIntent(createShareForecastIntent());
      }
    }
  }
  public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor data) {

    if (!data.moveToFirst() || data == null) return;

    int weatherId = data.getInt(COL_WEATHER_CONDITION_ID);
    // mIconView.setImageResource(R.drawable.ic_launcher);
    mIconView.setImageResource(Utility.getArtResourceForWeatherCondition(weatherId));

    long date = data.getLong(COL_WEATHER_DATE);
    String friendlyDateText = Utility.getDayName(getActivity(), date);
    String dateText = Utility.getFormattedMonthDay(getActivity(), date);
    mFriendlyDateView.setText(friendlyDateText);
    mDateView.setText(dateText);

    String description = data.getString(COL_WEATHER_DESC);
    mDescriptionView.setText(description);

    boolean isMetric = Utility.isMetric(getActivity());
    double high = data.getDouble(COL_WEATHER_MAX_TEMP);
    String highString = Utility.formatTemperature(getActivity(), high, isMetric);
    mHighTempView.setText(highString);

    double low = data.getDouble(COL_WEATHER_MIN_TEMP);
    String lowString = Utility.formatTemperature(getActivity(), low, isMetric);
    mLowTempView.setText(lowString);

    float humidity = data.getFloat(COL_WEATHER_HUMIDITY);
    mHumidityView.setText(getActivity().getString(R.string.format_humidity, humidity));

    float windSpeedStr = data.getFloat(COL_WEATHER_WIND_SPEED);
    float windDirStr = data.getFloat(COL_WEATHER_DEGREES);
    mWindView.setText(Utility.getFormattedWind(getActivity(), windSpeedStr, windDirStr));

    float pressure = data.getFloat(COL_WEATHER_PRESSURE);
    mPressureView.setText(getActivity().getString(R.string.format_pressure, pressure));

    mForecast = String.format("%s - %s - %s/%s", dateText, description, high, low);

    if (mShareActionProvider != null) {
      mShareActionProvider.setShareIntent(createShareForecastIntent());
    }
  }
    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
      Log.v(LOG_TAG, "In onLoadFinished");
      if (!data.moveToFirst()) {
        return;
      }

      String dateString =
          Utility.formatDate(data.getString(data.getColumnIndex(WeatherEntry.COLUMN_DATETEXT)));
      ((TextView) getView().findViewById(R.id.detail_date_textview)).setText(dateString);

      String weatherDescription =
          data.getString(data.getColumnIndex(WeatherEntry.COLUMN_SHORT_DESC));
      ((TextView) getView().findViewById(R.id.detail_forecast_textview))
          .setText(weatherDescription);

      boolean isMetric = Utility.isMetric(getActivity());

      String high =
          Utility.formatTemperature(
              data.getDouble(data.getColumnIndex(WeatherEntry.COLUMN_MAX_TEMP)), isMetric);
      ((TextView) getView().findViewById(R.id.detail_high_textview)).setText(high);

      String low =
          Utility.formatTemperature(
              data.getDouble(data.getColumnIndex(WeatherEntry.COLUMN_MIN_TEMP)), isMetric);
      ((TextView) getView().findViewById(R.id.detail_low_textview)).setText(low);

      // We still need this for the share intent
      mForecast = String.format("%s - %s - %s/%s", dateString, weatherDescription, high, low);

      Log.v(LOG_TAG, "Forecast String: " + mForecast);

      // If onCreateOptionsMenu has already happened, we need to update the share intent now.
      if (mShareActionProvider != null) {
        mShareActionProvider.setShareIntent(createShareForecastIntent());
      }
    }
  @Override
  public void onLoadFinished(Loader<Cursor> loader, Cursor data) {

    /*Log.v(LOG_TAG, "In onLoadFinished");
    if (!data.moveToFirst()) {
        return;
    }

    String date = Utility.formatDate(data.getLong(COL_WEATHER_DATE));
    String desc = data.getString(COL_WEATHER_DESC);
    Boolean ismetric = Utility.isMetric(getActivity());
    String max = Utility.formatTemperature(getActivity(), data.getDouble(COL_WEATHER_MAX_TEMP), ismetric);
    String min = Utility.formatTemperature(getActivity(), data.getDouble(COL_WEATHER_MIN_TEMP), ismetric);

    mForecast = String.format("%s - %s %s-%s", date, desc, max, min);

    TextView tv = (TextView) getView().findViewById(R.id.detail_text);
    tv.setText(mForecast);

    if (mShareActionProvider != null) {
        mShareActionProvider.setShareIntent(createShareForecastIntent());
    }*/

    Log.v(LOG_TAG, "In onLoadFinished");
    if (data != null && data.moveToFirst()) {
      // Read weather condition ID from cursor
      int weatherId = data.getInt(COL_WEATHER_CONDITION_ID);
      // Use placeholder Image
      // mIconView.setImageResource(Utility.getArtResourceForWeatherCondition(weatherId));
      Glide.with(this)
          .load(Utility.getArtUrlForWeatherCondition(getActivity(), weatherId))
          .error(Utility.getArtResourceForWeatherCondition(weatherId))
          .crossFade()
          .into(mIconView);

      // Read date from cursor and update views for day of week and date
      long date = data.getLong(COL_WEATHER_DATE);
      String friendlyDateText = Utility.getDayName(getActivity(), date);
      String dateText = Utility.getFormattedMonthDay(getActivity(), date);
      mFriendlyDateView.setText(friendlyDateText);
      mDateView.setText(dateText);

      // Read description from cursor and update view
      String description = data.getString(COL_WEATHER_DESC);
      mDescriptionView.setText(description);

      // Read high temperature from cursor and update view
      boolean isMetric = Utility.isMetric(getActivity());

      double high = data.getDouble(COL_WEATHER_MAX_TEMP);
      String highString = Utility.formatTemperature(getActivity(), high, isMetric);
      mHighTempView.setText(highString);

      // Read low temperature from cursor and update view
      double low = data.getDouble(COL_WEATHER_MIN_TEMP);
      String lowString = Utility.formatTemperature(getActivity(), low, isMetric);
      mLowTempView.setText(lowString);

      // Read humidity from cursor and update view
      float humidity = data.getFloat(COL_WEATHER_HUMIDITY);
      mHumidityView.setText(getActivity().getString(R.string.format_humidity, humidity));

      // Read wind speed and direction from cursor and update view
      float windSpeedStr = data.getFloat(COL_WEATHER_WIND_SPEED);
      float windDirStr = data.getFloat(COL_WEATHER_DEGREES);
      mWindView.setText(Utility.getFormattedWind(getActivity(), windSpeedStr, windDirStr));

      // Read pressure from cursor and update view
      float pressure = data.getFloat(COL_WEATHER_PRESSURE);
      mPressureView.setText(getActivity().getString(R.string.format_pressure, pressure));

      // We still need this for the share intent
      mForecast = String.format("%s - %s - %s/%s", dateText, description, high, low);

      // If onCreateOptionsMenu has already happened, we need to update the share intent now.
      if (mShareActionProvider != null) {
        mShareActionProvider.setShareIntent(createShareForecastIntent());
      }
    }
  }
 /** Prepare the weather high/lows for presentation. */
 private String formatHighLows(double high, double low) {
   boolean isMetric = Utility.isMetric(mContext);
   String highLowStr =
       Utility.formatTemperature(high, isMetric) + "/" + Utility.formatTemperature(low, isMetric);
   return highLowStr;
 }