Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Android Lists and Adapters (2015) Using Parcelable Data Retrieving Parcelable Data

Steven Nguyen
Steven Nguyen
2,413 Points

Problem with Parcelable data

Whenever I click "7 Day" to see the 7 day forecast, all the days are displayed as Saturday Day.java @Override public void writeToParcel(Parcel dest, int flags) { dest.writeLong(mTime); dest.writeString(mSummary); dest.writeDouble(mTemperatureMax); dest.writeString(mIcon); dest.writeString(mTimezone);

}

private Day(Parcel in){
    mTime = in.readLong();
    mSummary = in.readString();
    mTemperatureMax = in.readDouble();
    mIcon = in.readString();
    mTimezone = in.readString();
}

public Day() { }

public static final Creator<Day> CREATOR = new Creator<Day>() {
    @Override
    public Day createFromParcel(Parcel source) {
        return new Day(source);
    }

    @Override
    public Day[] newArray(int size) {
        return new Day[size];
    }
};

}

DailyForecastActivity.java

private Day[] mDays;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_daily_forecast);

    Intent intent = getIntent();
    Parcelable[] parcelables = intent.getParcelableArrayExtra(MainActivity.DAILY_FORECAST);
    mDays = Arrays.copyOf(parcelables, parcelables.length, Day[].class);

    DayAdapter adapter = new DayAdapter(this, mDays);

    setListAdapter(adapter);
}

}

DayAdapter.java

public class DayAdapter extends BaseAdapter {

private Context mContext;
private Day[] mDays;

public DayAdapter(Context context, Day[] days){
    mContext = context;
    mDays = days;

}

@Override
public int getCount() {
    return mDays.length;
}

@Override
public Object getItem(int position) {
    return mDays[position];
}

@Override
public long getItemId(int position) {
    return 0; //we arent going to use this; tag items for easy reference
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    if (convertView == null) {
        //brand new
        convertView = LayoutInflater.from(this.mContext).inflate(R.layout.daily_list_item, null);
        holder = new ViewHolder();
        holder.iconImageView = (ImageView) convertView.findViewById(R.id.iconImageView);
        holder.temperatureLabel = (TextView) convertView.findViewById(R.id.temperatureLabel);
        holder.dayLabel = (TextView) convertView.findViewById(R.id.dayNameLabel);
        holder.circleImageView = (ImageView) convertView.findViewById((R.id.circleImageView));

        convertView.setTag(holder);
    }
    else {
        holder = (ViewHolder) convertView.getTag();
    }

    Day day = mDays[position];

    holder.iconImageView.setImageResource(day.getIconId());
    holder.temperatureLabel.setText(day.getTemperatureMax() + "");
    holder.dayLabel.setText(day.getDayOfTheWeek());
    holder.circleImageView.setImageResource(R.drawable.bg_temperature);

    return convertView;
    }



private static class ViewHolder{
    ImageView iconImageView; //public by default
    TextView temperatureLabel;
    TextView dayLabel;
    ImageView circleImageView;

}

}

1 Answer

Emily Conroyd
Emily Conroyd
6,512 Points

2 things:

  1. try just passing in mContext instead of this.mContext for your layoutInflater
  2. don't set the circleImageView. In the video, he just has us pass 3 variables: a) iconImageView b) temperatureLabel c) dayLabel

Happy Coding! May the Force be with you!