Ejemplo n.º 1
0
  @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) {

    /*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());
      }
    }
  }
Ejemplo n.º 3
0
  @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
      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);

      // For accessibility, add a content description to the icon field
      mIconView.setContentDescription(getString(R.string.a11y_forecast_icon, 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);
      mHighTempView.setContentDescription(getString(R.string.a11y_high_temp, high));

      // 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);
      mLowTempView.setContentDescription(getString(R.string.a11y_low_temp, low));

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

      // 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));
      mWindView.setContentDescription(mWindView.getText());

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

      // 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());
      }
    }
  }