Android Open Source - Sunshine Forecast Adapter






From Project

Back to project page Sunshine.

License

The source code is released under:

MIT License

If you think the Android project Sunshine 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 cloudchen.com.sunshine;
/*from  w w  w . ja va 2s . c om*/
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import cloudchen.com.sunshine.data.WeatherContract;

public class ForecastAdapter extends CursorAdapter {

    private final int VIEW_TYPE_TODAY = 0;
    private final int VIEW_TYPE_FUTURE_TODAY = 1;
    private boolean mUseTodayLayout;

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

    public void setUseTodayLayout(boolean useTodayLayout) {
        mUseTodayLayout = useTodayLayout;
    }

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

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

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        int viewType = getItemViewType(cursor.getPosition());
        int layoutId = (viewType == VIEW_TYPE_TODAY) ? R.layout.list_item_forecast_today : R.layout.list_item_forecast;
        return LayoutInflater.from(context).inflate(layoutId, parent, false);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ViewHolder holder;

        if (view.getTag() != null) {
            holder = (ViewHolder) view.getTag();
        } else {
            holder = new ViewHolder();
            holder.icon = (ImageView) view.findViewById(R.id.list_item_icon);
            holder.date = (TextView) view.findViewById(R.id.list_item_date_textview);
            holder.description = (TextView) view.findViewById(R.id.list_item_forecast_textview);
            holder.high = (TextView) view.findViewById(R.id.list_item_high_textview);
            holder.low = (TextView) view.findViewById(R.id.list_item_low_textview);
        }
        // Read weather icon ID from cursor
        int weatherId = cursor.getInt(ForecastFragment.COL_WEATHER_CONDITION_ID);
        int viewType = getItemViewType(cursor.getPosition());
        switch (viewType) {
            case VIEW_TYPE_TODAY:
                holder.icon.setImageResource(Utility.getArtResourceForWeatherCondition(weatherId));
                break;
            case VIEW_TYPE_FUTURE_TODAY:
                holder.icon.setImageResource(Utility.getIconResourceForWeatherCondition(weatherId));
                break;
        }

        // Read date from cursor
        String dateString = cursor.getString(ForecastFragment.COL_WEATHER_DATE);
        holder.date.setText(Utility.getFriendlyDayString(context, dateString));

        // Read weather forecast from cursor
        String description = cursor.getString(ForecastFragment.COL_WEATHER_DESC);
        holder.description.setText(description);

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

        // Read high temperature from cursor
        float high = cursor.getFloat(ForecastFragment.COL_WEATHER_MAX_TEMP);
        holder.high.setText(Utility.formatTemperature(context, high, isMetric));

        // Read low temperature from cursor
        float low = cursor.getFloat(ForecastFragment.COL_WEATHER_MIN_TEMP);
        holder.low.setText(Utility.formatTemperature(context, low, isMetric));
    }

    class ViewHolder {
        ImageView icon;
        TextView date;
        TextView high;
        TextView low;
        TextView description;
    }
}




Java Source Code List

cloudchen.com.sunshine.ApplicationTest.java
cloudchen.com.sunshine.DetailActivity.java
cloudchen.com.sunshine.DetailFragment.java
cloudchen.com.sunshine.ForecastAdapter.java
cloudchen.com.sunshine.ForecastFragment.java
cloudchen.com.sunshine.FullTestSuite.java
cloudchen.com.sunshine.MainActivity.java
cloudchen.com.sunshine.SettingsActivity.java
cloudchen.com.sunshine.TestDb.java
cloudchen.com.sunshine.TestProvider.java
cloudchen.com.sunshine.Utility.java
cloudchen.com.sunshine.data.WeatherContract.java
cloudchen.com.sunshine.data.WeatherDbHelper.java
cloudchen.com.sunshine.data.WeatherProvider.java
cloudchen.com.sunshine.sync.SunshineAuthenticatorService.java
cloudchen.com.sunshine.sync.SunshineAuthenticator.java
cloudchen.com.sunshine.sync.SunshineSyncAdapter.java
cloudchen.com.sunshine.sync.SunshineSyncService.java