nintao.com.android.study.sunshine.ForecastAdapter.java Source code

Java tutorial

Introduction

Here is the source code for nintao.com.android.study.sunshine.ForecastAdapter.java

Source

package nintao.com.android.study.sunshine;

/*
* The following code a based on Udacity Android class content.
* Necessary changes made to fit my own written code.
* Still follows original license since most of this code are from Udacity.
 */

import android.util.Log;
import android.view.View;
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 org.w3c.dom.Text;

import nintao.com.android.study.sunshine.data.WeatherContract;

/**
 * {@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 {

    private final String LOG_TAG = WeatherFragment.class.getSimpleName();
    // These indices are tied to FORECAST_COLUMNS.  If FORECAST_COLUMNS changes, these
    // must change.
    static final int COL_WEATHER_ID = 0;
    static final int COL_WEATHER_DATE = 1;
    static final int COL_WEATHER_DESC = 2;
    static final int COL_WEATHER_MAX_TEMP = 3;
    static final int COL_WEATHER_MIN_TEMP = 4;
    static final int COL_LOCATION_SETTING = 5;
    static final int COL_WEATHER_CONDITION_ID = 6;
    static final int COL_COORD_LAT = 7;
    static final int COL_COORD_LONG = 8;

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

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

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

    /*
    This is ported from FetchWeatherTask --- but now we go straight from the cursor to the
    string.
     */
    private String convertCursorRowToUXFormat(Cursor cursor) {

        //        // get row indices for our cursor
        //        int idx_max_temp = cursor.getColumnIndex(WeatherContract.WeatherEntry.COLUMN_MAX_TEMP);
        //        int idx_min_temp = cursor.getColumnIndex(WeatherContract.WeatherEntry.COLUMN_MIN_TEMP);
        //        int idx_date = cursor.getColumnIndex(WeatherContract.WeatherEntry.COLUMN_DATE);
        //        int idx_short_desc = cursor.getColumnIndex(WeatherContract.WeatherEntry.COLUMN_SHORT_DESC);

        String highAndLow = formatHighLows(cursor.getDouble(COL_WEATHER_MAX_TEMP),
                cursor.getDouble(COL_WEATHER_MIN_TEMP));

        return Utility.formatDate(cursor.getLong(COL_WEATHER_DATE)) + " - " + cursor.getString(COL_WEATHER_DESC)
                + " - " + highAndLow;
    }

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

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

    /*
    Remember that these views are reused as needed.
     */
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {

        //choose layout type
        int viewType = getItemViewType(cursor.getPosition());
        int layoutId = 0;
        //determine the layoutId from viewType;
        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;
        } else {
            Log.w(LOG_TAG, "Didn't match any list item view.");
        }
        //inflate the corresponding layout
        View view = LayoutInflater.from(context).inflate(layoutId, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        view.setTag(viewHolder);

        return view;
    }

    /*
    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.

        //        removed when the list item is updated.
        //        TextView tv = (TextView)view;
        //        tv.setText(convertCursorRowToUXFormat(cursor));

        //fetch the weather id from cursor
        //        int weatherId = cursor.getInt(COL_WEATHER_ID);

        //get viewTag
        ViewHolder viewHolder = (ViewHolder) view.getTag();

        //read image icon
        viewHolder.iconView.setImageResource(R.drawable.ic_launcher);

        //read date from cursor
        long date = cursor.getLong(COL_WEATHER_DATE);
        //        TextView dateView = (TextView) view.findViewById(R.id.list_item_day_textview);
        //        dateView.setText(Utility.getFriendlyDayString(context, date));
        viewHolder.dateView.setText(Utility.getFriendlyDayString(context, date));

        //read weather description
        String description = cursor.getString(COL_WEATHER_DESC);
        //        TextView descriptionView = (TextView) view.findViewById(R.id.list_item_forecast_textview);
        //        descriptionView.setText(description);
        viewHolder.descriptionView.setText(description);

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

        //read high temp
        double high = cursor.getDouble(COL_WEATHER_MAX_TEMP);
        //TextView highView = (TextView) view.findViewById(R.id.list_item_forecast_high_textview);
        //        highVieww.setText(Utility.formatTemperature(high, isMetric));
        viewHolder.highTempView.setText(Utility.formatTemperature(mContext, high, isMetric));

        //read low temp
        double low = cursor.getDouble(COL_WEATHER_MIN_TEMP);
        //        TextView lowView = (TextView) view.findViewById(R.id.list_item_forecast_low_textview);
        //        lowView.setText(Utility.formatTemperature(low, isMetric));
        viewHolder.lowTempView.setText(Utility.formatTemperature(mContext, low, isMetric));

    }

    /**
     * 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_day_textview);
            descriptionView = (TextView) view.findViewById(R.id.list_item_forecast_textview);
            highTempView = (TextView) view.findViewById(R.id.list_item_forecast_high_textview);
            lowTempView = (TextView) view.findViewById(R.id.list_item_forecast_low_textview);
        }
    }
}