com.example.asadkhan.sunshine.ForecastAdapter.java Source code

Java tutorial

Introduction

Here is the source code for com.example.asadkhan.sunshine.ForecastAdapter.java

Source

/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.asadkhan.sunshine;

import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.util.Log;
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 Cursor} to a {@link android.widget.ListView}.
 */
public class ForecastAdapter extends CursorAdapter {

    private final static String LOG_TAG = ForecastAdapter.class.getSimpleName();

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

    private final int VIEW_TYPE_COUNT = 2;

    private boolean mUseTodayLayout = true;

    public ForecastAdapter(Context context) {
        super(context, null, 0);
    }

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

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

    public void setUseTodayLayout(boolean UseTodayLayout) {
        mUseTodayLayout = !UseTodayLayout;
    }

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

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

        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;
    }

    /*
    This is where we fill-in the views with the contents of the cursor.
     */
    @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(MainForecastFragment.COL_WEATHER_CONDITION_ID)));
            break;
        }
        case VIEW_TYPE_FUTURE_DAY: {
            viewHolder.iconView.setImageResource(Utility.getIconResourceForWeatherCondition(
                    cursor.getInt(MainForecastFragment.COL_WEATHER_CONDITION_ID)));
            break;
        }
        }

        Long dateInMillis = cursor.getLong(MainForecastFragment.COL_WEATHER_DATE);
        viewHolder.dateView.setText(Utility.getFriendlyDayString(context, dateInMillis));

        String description = cursor.getString(MainForecastFragment.COL_WEATHER_DESC);
        viewHolder.descriptionView.setText(description);

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

        double high = cursor.getDouble(MainForecastFragment.COL_WEATHER_MAX_TEMP);
        viewHolder.highTempView.setText(Utility.formatTemperature(context, high, isMetric));

        double low = cursor.getDouble(MainForecastFragment.COL_WEATHER_MIN_TEMP);
        viewHolder.lowTempView.setText(Utility.formatTemperature(context, low, isMetric));

        // Log.e(LOG_TAG, "High val : " + high);
        // Log.e(LOG_TAG, "Low val : " + low);
    }

    /**
     * Cache of the children views for a forecast list item.
     */
    private static class ViewHolder {

        /* Constituent views */
        private final ImageView iconView;
        private final TextView dateView;
        private final TextView descriptionView;
        private final TextView highTempView;
        private final TextView lowTempView;

        /* Public Constructor */
        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);
        }
    }

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

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

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

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

}