package com.example.android.sunshine2;

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;

/**
 * {@link ForecastAdapter} exposes a list of weather forecasts
 * from a {@link android.database.Cursor} to a {@link android.widget.ListView}.
 */
public class ForecastAdapter extends CursorAdapter {
    public ForecastAdapter(Context context, Cursor c, int flags) {
        super(context, c, flags);
    }

    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;

    /*
        Remember that these views are reused as needed.
     */
    @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;
            }
        }
        return LayoutInflater.from(context).inflate(layoutId, parent, false);
    }

    /*
        This is where we fill-in the views with the contents of the cursor.
     */
    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        // our view is pretty simple here --- just a text view
        // we'll keep the UI functional with a simple (and slow!) binding.

        // Read weather icon ID from cursor
        int weatherId = cursor.getInt(ForecastFragment.COL_WEATHER_ID);
        // Use placeholder image for now
        ImageView iconView = (ImageView) view.findViewById(R.id.list_item_icon);
        iconView.setImageResource(R.drawable.ic_launcher);

        // Read date from cursor
        long dateInMillis = cursor.getLong(ForecastFragment.COL_WEATHER_DATE);
        // Find TextView and set formatted date on it
        TextView dateView = (TextView) view.findViewById(R.id.list_item_date_textview);
        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
        TextView descriptionView = (TextView) view.findViewById(R.id.list_item_forecast_textview);
        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);
        TextView highView = (TextView) view.findViewById(R.id.list_item_high_textview);
        highView.setText(Utility.formatTemperature(high, isMetric));

        // Read low temperature from cursor
        double low = cursor.getDouble(ForecastFragment.COL_WEATHER_MIN_TEMP);
        TextView lowView = (TextView) view.findViewById(R.id.list_item_low_textview);
        lowView.setText(Utility.formatTemperature(low, isMetric));
    }

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

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

@@ -388,33 +388,16 @@
  %7B@link 
-android.database.
 Cursor%7D 
@@ -494,115 +494,8 @@
 er %7B
-%0A    public ForecastAdapter(Context context, Cursor c, int flags) %7B%0A        super(context, c, flags);%0A    %7D
 %0A%0A  
@@ -658,72 +658,919 @@
   /*
+*
 %0A     
-   Remember that these views are reused as needed.%0A     */
+* Cache of the children views for a forecast list item.%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%0A    public ForecastAdapter(Context context, Cursor c, int flags) %7B%0A        super(context, c, flags);%0A    %7D%0A
 %0A   
@@ -2086,24 +2086,25 @@
       %7D%0A
+%0A
         
 return L
@@ -2095,22 +2095,27 @@
         
-return
+View view =
  LayoutI
@@ -2174,105 +2174,124 @@
 e);%0A
-    %7D%0A%0A    /*%0A        This is where we fill-in the views with the contents of the cursor.%0A 
+%0A        ViewHolder viewHolder = new ViewHolder(view);%0A        view.setTag(viewHolder);%0A%0A        return view;%0A
     
-*/
+%7D%0A
 %0A   
@@ -2371,24 +2371,25 @@
 rsor) %7B%0A
+%0A
         
 // our v
@@ -2384,255 +2384,61 @@
     
-// our view is pretty simple here --- just a text view%0A        // we'll keep the UI functional with a simple (and slow!) binding.%0A%0A        // Read weather icon ID from cursor%0A        int weatherId = cursor.getInt(ForecastFragment.COL_WEATHER_ID
+ViewHolder viewHolder = (ViewHolder) view.getTag(
 );%0A
+%0A
     
@@ -2486,89 +2486,19 @@
     
-ImageView iconView = (ImageView) view.findViewById(R.id.list_item_icon);%0A        
+viewHolder.
 icon
@@ -2723,96 +2723,19 @@
     
-TextView dateView = (TextView) view.findViewById(R.id.list_item_date_textview);%0A        
+viewHolder.
 date
@@ -2997,107 +2997,19 @@
     
-TextView descriptionView = (TextView) view.findViewById(R.id.list_item_forecast_textview);%0A        
+viewHolder.
 desc
@@ -3307,100 +3307,27 @@
     
-TextView highView = (TextView) view.findViewById(R.id.list_item_high_textview);%0A        high
+viewHolder.highTemp
 View
@@ -3514,97 +3514,26 @@
     
-TextView lowView = (TextView) view.findViewById(R.id.list_item_low_textview);%0A        low
+viewHolder.lowTemp
 View

use viewholder