Android Open Source - SunshineApp Forecast Adapter






From Project

Back to project page SunshineApp.

License

The source code is released under:

Apache License

If you think the Android project SunshineApp 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 sunshine.ferdyrodriguez.com.sunshine;
/*from   www  . j av a2 s .c om*/
import android.app.Fragment;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

/**
 * Created by ferdy.rodriguez on 05/09/2014.
 */
public class ForecastAdapter extends CursorAdapter {

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

    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;

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

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

    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;
        // TODO: Determine 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;
        }

        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 weatherID = cursor.getInt(ForecastFragment.COL_WEATHER_CONDITION_ID);
        Log.v(TAG, "Weather ID code: " + weatherID);
        int viewType = getItemViewType(cursor.getPosition());
        switch (viewType) {
            case VIEW_TYPE_TODAY:
                viewHolder.iconView.setImageResource(Utility.getArtResourceForWeatherCondition(weatherID));
                break;
            case VIEW_TYPE_FUTURE_DAY:
                viewHolder.iconView.setImageResource(Utility.getIconResourceForWeatherCondition(weatherID));
                break;
        }

        String dateString = cursor.getString(ForecastFragment.COL_WEATHER_DATE);
        viewHolder.dateView.setText(Utility.getFriendlyDayString(context, dateString));

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

        boolean isMetric = Utility.isMetric(context);

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

        double low = cursor.getDouble(ForecastFragment.COL_WEATHER_MIN_TEMP);
        viewHolder.lowView.setText(Utility.formatTemperature(context, low, isMetric));
    }


    public static class ViewHolder {
        public final ImageView iconView;
        public final TextView dateView;
        public final TextView descriptionView;
        public final TextView highView;
        public final TextView lowView;

        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);
            highView = (TextView) view.findViewById(R.id.list_item_high_textview);
            lowView = (TextView) view.findViewById(R.id.list_item_low_textview);
        }
    }

}




Java Source Code List

sunshine.ferdyrodriguez.com.sunshine.ApplicationTest.java
sunshine.ferdyrodriguez.com.sunshine.DetailActivity.java
sunshine.ferdyrodriguez.com.sunshine.DetailFragment.java
sunshine.ferdyrodriguez.com.sunshine.FetchWeatherTask.java
sunshine.ferdyrodriguez.com.sunshine.ForecastAdapter.java
sunshine.ferdyrodriguez.com.sunshine.ForecastFragment.java
sunshine.ferdyrodriguez.com.sunshine.MainActivity.java
sunshine.ferdyrodriguez.com.sunshine.SettingsActivity.java
sunshine.ferdyrodriguez.com.sunshine.TestDB.java
sunshine.ferdyrodriguez.com.sunshine.TestProvider.java
sunshine.ferdyrodriguez.com.sunshine.Utility.java
sunshine.ferdyrodriguez.com.sunshine.data.WeatherContract.java
sunshine.ferdyrodriguez.com.sunshine.data.WeatherDbHelper.java
sunshine.ferdyrodriguez.com.sunshine.data.WeatherProvider.java