package com.example.android.sunshine.app;

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;

public class ForecastAdapter extends CursorAdapter {

    private final int VIEW_TYPE_TODAY = 0;
    private final int VIEW_TYPE_FUTURE_DAY = 1;

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

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        int viewType = getItemViewType(cursor.getPosition());

        int layoutId = -1;

        if (viewType == VIEW_TYPE_TODAY) {
            layoutId = R.layout.list_item_forecast_today;
        } else if (viewType == VIEW_TYPE_FUTURE_DAY) {
            layoutId = R.layout.list_item_forecast;
        }

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

        return view;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ImageView iconView = (ImageView) view.findViewById(R.id.list_item_icon);
        TextView dateView = (TextView) view.findViewById(R.id.list_item_date_textview);
        TextView forecastView = (TextView) view.findViewById(R.id.list_item_forecast_textview);
        TextView highView = (TextView) view.findViewById(R.id.list_item_high_textview);
        TextView lowView = (TextView) view.findViewById(R.id.list_item_low_textview);

        int iconId = cursor.getInt(MainFragment.COL_WEATHER_ID);
        double high = cursor.getDouble(MainFragment.COL_WEATHER_MAX_TEMP);
        double low = cursor.getDouble(MainFragment.COL_WEATHER_MIN_TEMP);
        String forecast = cursor.getString(MainFragment.COL_WEATHER_DESC);
        long dateInMillis = cursor.getLong(MainFragment.COL_WEATHER_DATE);

        boolean isMetric = Utility.isMetric(context);

        iconView.setImageResource(R.drawable.ic_launcher);
        dateView.setText(Utility.formatDate(dateInMillis));
        forecastView.setText(forecast);
        highView.setText(Utility.formatTemperature(high, isMetric));
        lowView.setText(Utility.formatTemperature(low, isMetric));
    }

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

    @Override
    public int getItemViewType(int position) {
        return (position == 0) ? VIEW_TYPE_TODAY : VIEW_TYPE_FUTURE_DAY;
    }
}
@@ -1041,24 +1041,112 @@
 t, false);%0A%0A
+        ViewHolder viewHolder = new ViewHolder(view);%0A        view.setTag(viewHolder);%0A%0A
         retu
@@ -1257,436 +1257,57 @@
     
-ImageView iconView = (ImageView) view.findViewById(R.id.list_item_icon);%0A        TextView dateView = (TextView) view.findViewById(R.id.list_item_date_textview);%0A        TextView forecastView = (TextView) view.findViewById(R.id.list_item_forecast_textview);%0A        TextView highView = (TextView) view.findViewById(R.id.list_item_high_textview);%0A        TextView lowView = (TextView) view.findViewById(R.id.list_item_low_textview
+ViewHolder viewHolder = (ViewHolder) view.getTag(
 );%0A%0A
@@ -1535,24 +1535,27 @@
  String 
-forecast
+description
  = curso
@@ -1737,16 +1737,27 @@
         
+viewHolder.
 iconView
@@ -1807,16 +1807,27 @@
         
+viewHolder.
 dateView
@@ -1882,52 +1882,84 @@
     
-forecastView.setText(forecast);%0A        high
+viewHolder.descriptionView.setText(description);%0A        viewHolder.highTemp
 View
@@ -2019,19 +2019,34 @@
         
-low
+viewHolder.lowTemp
 View.set
@@ -2314,10 +2314,743 @@
 Y;%0A    %7D
+%0A%0A    public static class ViewHolder %7B%0A        public final ImageView iconView;%0A        public final TextView dateView;%0A        public final TextView descriptionView;%0A        public final TextView highTempView;%0A        public final TextView lowTempView;%0A%0A        public ViewHolder(View view) %7B%0A            iconView = (ImageView) view.findViewById(R.id.list_item_icon);%0A            dateView = (TextView) view.findViewById(R.id.list_item_date_textview);%0A            descriptionView = (TextView) view.findViewById(R.id.list_item_forecast_textview);%0A            highTempView = (TextView) view.findViewById(R.id.list_item_high_textview);%0A            lowTempView = (TextView) view.findViewById(R.id.list_item_low_textview);%0A        %7D%0A    %7D
 %0A%7D

Use ViewHolder to access list item views