@Override
    protected Void doInBackground(Cursor... params) {

      Cursor cursor = params[0];
      cursor.moveToPosition(0);
      double high = cursor.getDouble(ForecastFragment.COL_WEATHER_MAX_TEMP);
      String max = Utility.formatTemperature(getActivity(), high);
      double low = cursor.getDouble(ForecastFragment.COL_WEATHER_MIN_TEMP);
      String min = Utility.formatTemperature(getActivity(), low);
      int weatherId = cursor.getInt(ForecastFragment.COL_WEATHER_CONDITION_ID);
      Bitmap weatherIconBitmap = null;

      try {
        weatherIconBitmap =
            Glide.with(getActivity())
                .load(Utility.getArtUrlForWeatherCondition(getActivity(), weatherId))
                .asBitmap()
                .into(100, 100)
                . // Width and height
                get();

        ((TodayDataCallback) getActivity()).onTodayDataLoaded(max, min, weatherIconBitmap);

      } catch (InterruptedException e) {
        e.printStackTrace();
      } catch (ExecutionException e) {
        e.printStackTrace();
      }

      return null;
    }
  @Override
  public void bindView(View view, Context context, Cursor cursor) {

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

    // Read weather icon ID from cursor
    int weatherId = cursor.getInt(ForecastFragment.COL_WEATHER_ID);
    String dateString = cursor.getString(ForecastFragment.COL_WEATHER_DATE);
    String description = cursor.getString(ForecastFragment.COL_WEATHER_DESC);
    boolean isMetric = Utility.isMetric(context);
    float high = cursor.getFloat(ForecastFragment.COL_WEATHER_MAX_TEMP);
    float low = cursor.getFloat(ForecastFragment.COL_WEATHER_MIN_TEMP);

    //        viewHolder.iconView.setImageResource(R.mipmap.ic_launcher);
    viewHolder.dateView.setText(Utility.getFriendlyDayString(context, dateString));
    viewHolder.descriptionView.setText(description);

    viewHolder.highTempView.setText(Utility.formatTemperature(context, high, isMetric));
    viewHolder.lowTempView.setText(Utility.formatTemperature(context, low, isMetric));

    if (getItemViewType(cursor.getPosition()) == VIEW_TYPE_TODAY) {
      int artResource =
          Utility.getArtResourceForWeatherCondition(
              cursor.getInt(cursor.getColumnIndex(WeatherContract.WeatherEntry.COLUMN_WEATHER_ID)));
      viewHolder.iconView.setImageResource(artResource);
    } else {
      int iconResource =
          Utility.getIconResourceForWeatherCondition(
              cursor.getInt(cursor.getColumnIndex(WeatherContract.WeatherEntry.COLUMN_WEATHER_ID)));
      viewHolder.iconView.setImageResource(iconResource);
    }
  }
Beispiel #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(data.getColumnIndex(WeatherEntry.COLUMN_WEATHER_ID));
      // Use placeholder Image
      mIconView.setImageResource(Utility.getArtResourceForWeatherCondition(weatherId));

      // Read date from cursor and update views for day of week and date
      String date = data.getString(data.getColumnIndex(WeatherEntry.COLUMN_DATETEXT));
      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(data.getColumnIndex(WeatherEntry.COLUMN_SHORT_DESC));
      mDescriptionView.setText(description);

      // For accessibility, add a content description to the icon field.
      mIconView.setContentDescription(description);

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

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

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

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

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

      // Read pressure from cursor and update view
      float pressure = data.getFloat(data.getColumnIndex(WeatherEntry.COLUMN_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());
      }
    }
  }
  @Override
  public void bindView(View view, Context context, Cursor cursor) {

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

    int viewType = getItemViewType(cursor.getPosition());
    switch (viewType) {
      case VIEW_TYPE_TODAY:
        viewHolder.iconView.setImageResource(
            Utility.getArtResourceForWeatherCondition(
                cursor.getInt(ForecastFragment.COL_WEATHER_CONDITION_ID)));
        break;
      case VIEW_TYPE_FUTURE_DAY:
        viewHolder.iconView.setImageResource(
            Utility.getIconResourceForWeatherCondition(
                cursor.getInt(ForecastFragment.COL_WEATHER_CONDITION_ID)));
        break;
    }

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

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

    // Read high temperature from cursor
    double high = cursor.getDouble(ForecastFragment.COL_WEATHER_MAX_TEMP);
    viewHolder.highTempView.setText(Utility.formatTemperature(context, high));

    // Read low temperature from cursor
    double low = cursor.getDouble(ForecastFragment.COL_WEATHER_MIN_TEMP);
    viewHolder.lowTempView.setText(Utility.formatTemperature(context, low));
  }
  /*
     This is where we fill-in the views with the contents of the cursor.
  */
  @Override
  public void bindView(View view, Context context, Cursor cursor) {

    int position = cursor.getPosition();
    int viewType = getItemViewType(position);

    long date = cursor.getLong(ForecastAdapter.COL_WEATHER_DATE);
    double min = cursor.getDouble(COL_WEATHER_MIN_TEMP);
    double max = cursor.getDouble(COL_WEATHER_MAX_TEMP);
    String desc = cursor.getString(COL_WEATHER_DESC);
    boolean isMetric = Utility.isMetric(context);

    ViewHolder viewHolder = (ViewHolder) view.getTag();
    if (viewType == VIEW_TYPE_TODAY) {
      viewHolder.imageIcon.setImageResource(Utility.getImageResourceToday(desc));
    } else {
      viewHolder.imageIcon.setImageResource(Utility.getImageResource(desc));
    }
    viewHolder.date.setText(Utility.getFriendlyDayString(context, date));
    viewHolder.lowTemperature.setText(Utility.formatTemperature(mContext, min, isMetric));
    viewHolder.highTemperature.setText(Utility.formatTemperature(mContext, max, isMetric));
    viewHolder.description.setText(desc);
  }
    /**
     * Take the String representing the complete forecast in JSON Format and pull out the data we
     * need to construct the Strings needed for the wireframes.
     *
     * <p>Fortunately parsing is easy: constructor takes the JSON string and converts it into an
     * Object hierarchy for us.
     */
    private ForeCastObj[] getWeatherDataFromJson(String forecastJsonStr, int numDays)
        throws JSONException {

      // These are the names of the JSON objects that need to be extracted.
      final String OWM_LIST = "list";
      final String OWM_WEATHER = "weather";
      final String OWM_TEMPERATURE = "temp";
      final String OWM_MAX = "max";
      final String OWM_MIN = "min";
      final String OWM_DESCRIPTION = "main";

      JSONObject forecastJson = new JSONObject(forecastJsonStr);
      JSONArray weatherArray = forecastJson.getJSONArray(OWM_LIST);

      // OWM returns daily forecasts based upon the local time of the city that is being
      // asked for, which means that we need to know the GMT offset to translate this data
      // properly.

      // Since this data is also sent in-order and the first day is always the
      // current day, we're going to take advantage of that to get a nice
      // normalized UTC date for all of our weather.

      Time dayTime = new Time();
      dayTime.setToNow();

      // we start at the day returned by local time. Otherwise this is a mess.
      int julianStartDay = Time.getJulianDay(System.currentTimeMillis(), dayTime.gmtoff);

      // now we work exclusively in UTC
      dayTime = new Time();

      ForeCastObj[] resultStrs = new ForeCastObj[numDays];
      for (int i = 0; i < weatherArray.length(); i++) {
        // For now, using the format "Day, description, hi/low"
        String day;
        String description;
        String highAndLow;

        // Get the JSON object representing the day
        JSONObject dayForecast = weatherArray.getJSONObject(i);

        // The date/time is returned as a long.  We need to convert that
        // into something human-readable, since most people won't read "1400356800" as
        // "this saturday".
        long dateTime;
        // Cheating to convert this to UTC time, which is what we want anyhow
        dateTime = dayTime.setJulianDay(julianStartDay + i);
        // day = getReadableDateString(dateTime);
        day = Utility.getDayName(getApplicationContext(), dateTime);

        // description is in a child array called "weather", which is 1 element long.
        JSONObject weatherObject = dayForecast.getJSONArray(OWM_WEATHER).getJSONObject(0);
        description = weatherObject.getString(OWM_DESCRIPTION);

        // Temperatures are in a child object called "temp".  Try not to name variables
        // "temp" when working with temperature.  It confuses everybody.
        JSONObject temperatureObject = dayForecast.getJSONObject(OWM_TEMPERATURE);
        double high = temperatureObject.getDouble(OWM_MAX);
        double low = temperatureObject.getDouble(OWM_MIN);

        ForeCastObj tmpObj = new ForeCastObj();
        tmpObj.day = day;
        tmpObj.description = description;
        Log.d(LOG_TAG, Unit);
        tmpObj.hight =
            Utility.formatTemperature(
                getApplicationContext(), high, (Unit == "metric" ? false : true));
        tmpObj.low =
            Utility.formatTemperature(
                getApplicationContext(), low, (Unit == "metric" ? false : true));

        resultStrs[i] = tmpObj;
      }

      return resultStrs;
    }
 /** Prepare the weather high/lows for presentation. */
 private String formatHighLows(double high, double low) {
   boolean isMetric = Utility.isMetric();
   String highLowStr =
       Utility.formatTemperature(high, isMetric) + "/" + Utility.formatTemperature(low, isMetric);
   return highLowStr;
 }