Android Open Source - Weather-app Forecast Adapter






From Project

Back to project page Weather-app.

License

The source code is released under:

Apache License

If you think the Android project Weather-app 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 app.sunshine.juanjo.views.adapter;
//from w  ww .  j  ava  2 s .  c  om
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import app.sunshine.juanjo.R;
import app.sunshine.juanjo.util.Utility;
import app.sunshine.juanjo.views.fragments.ForecastFragment;

/**
 * {@link ForecastAdapter} exposes a list of weather forecasts from a
 * {@link Cursor} to a {@link android.widget.ListView}.
 */
public class ForecastAdapter extends CursorAdapter {

  private static final int VIEW_TYPE_COUNT = 2;
  private static final int VIEW_TYPE_TODAY = 0;
  private static final int VIEW_TYPE_FUTURE_DAY = 1;

  // Flag to determine if we want to use a separate view for "today".
  private boolean mUseTodayLayout = true;

  /**
   * Cache of the children views for a forecast list item.
   */
  public static class ViewHolder {
    public final ImageView iconView;
    public final TextView dateView;
    public final TextView descriptionView;
    public final TextView highTempView;
    public final TextView lowTempView;

    public ViewHolder(View view) {
      iconView = (ImageView) view.findViewById(R.id.list_item_icon);
      dateView = (TextView) view.findViewById(R.id.list_item_date_textview);
      descriptionView = (TextView) view.findViewById(R.id.list_item_forecast_textview);
      highTempView = (TextView) view.findViewById(R.id.list_item_high_textview);
      lowTempView = (TextView) view.findViewById(R.id.list_item_low_textview);
    }
  }

  public ForecastAdapter(Context context, Cursor c, int flags) {
    super(context, c, flags);
  }

  @Override
  public View newView(Context context, Cursor cursor, ViewGroup parent) {
    // Choose the layout type
    int viewType = getItemViewType(cursor.getPosition());
    int layoutId = -1;
    switch (viewType) {
    case VIEW_TYPE_TODAY: {
      layoutId = R.layout.list_item_forecast_today;
      break;
    }
    case VIEW_TYPE_FUTURE_DAY: {
      layoutId = R.layout.list_item_forecast;
      break;
    }
    }

    View view = LayoutInflater.from(context).inflate(layoutId, parent, false);

    ViewHolder viewHolder = new ViewHolder(view);
    view.setTag(viewHolder);

    return view;
  }

  @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: {
      // Get weather icon
      viewHolder.iconView.setImageResource(Utility.getArtResourceForWeatherCondition(cursor
          .getInt(ForecastFragment.COL_WEATHER_CONDITION_ID)));
      break;
    }
    case VIEW_TYPE_FUTURE_DAY: {
      // Get weather icon
      viewHolder.iconView.setImageResource(Utility.getIconResourceForWeatherCondition(cursor
          .getInt(ForecastFragment.COL_WEATHER_CONDITION_ID)));
      break;
    }
    }

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

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

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

    // Read low temperature from cursor
    double low = cursor.getDouble(ForecastFragment.COL_WEATHER_MIN_TEMP);
    viewHolder.lowTempView.setText(Utility.formatTemperature(context, low, isMetric));
  }

  @Override
  public int getItemViewType(int position) {
    // return position == 0 ? VIEW_TYPE_TODAY : VIEW_TYPE_FUTURE_DAY;
    return (position == 0 && mUseTodayLayout) ? VIEW_TYPE_TODAY : VIEW_TYPE_FUTURE_DAY;
  }

  public void setUseTodayLayout(boolean useTodayLayout) {
    mUseTodayLayout = useTodayLayout;
  }

  @Override
  public int getViewTypeCount() {
    return VIEW_TYPE_COUNT;
  }
}




Java Source Code List

app.sunshine.juanjo.gcm.GcmBroadcastReceiver.java
app.sunshine.juanjo.gcm.GcmIntentService.java
app.sunshine.juanjo.network.APIHTTP.java
app.sunshine.juanjo.network.HttpRequest.java
app.sunshine.juanjo.network.SendIdGCM.java
app.sunshine.juanjo.sync.SunshineAuthenticatorService.java
app.sunshine.juanjo.sync.SunshineAuthenticator.java
app.sunshine.juanjo.sync.SunshineSyncAdapter.java
app.sunshine.juanjo.sync.SunshineSyncService.java
app.sunshine.juanjo.util.Utility.java
app.sunshine.juanjo.util.WeatherContract.java
app.sunshine.juanjo.util.WeatherDbHelper.java
app.sunshine.juanjo.util.WeatherJsonParser.java
app.sunshine.juanjo.util.WeatherProvider.java
app.sunshine.juanjo.views.MyView.java
app.sunshine.juanjo.views.activities.DetailActivity.java
app.sunshine.juanjo.views.activities.MainActivity.java
app.sunshine.juanjo.views.activities.SettingsActivity.java
app.sunshine.juanjo.views.adapter.ForecastAdapter.java
app.sunshine.juanjo.views.fragments.DetailFragment.java
app.sunshine.juanjo.views.fragments.ForeCastFragment.java