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

James N
James N
17,864 Points

my list view is EMPTY!!!!

hey! My App Will Not Load Whatever Data Is In the 7 Day mode! when I tap 7 day, I get the "There Is No Data To display" Textview.

Dayadapter.java
package james.stormy.Adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import james.stormy.R;
import james.stormy.weather.Day;

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; // don't use
    }

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

        if (convertView == null) {
            //new
            convertView = LayoutInflater.from(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);

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

        Day day = mDays[position];

        holder.iconImageView.setImageResource(day.getIconId());
        holder.temperatureLabel.setText(day.getTempMax() + "");
        holder.dayLabel.setText(day.getDayWeek());

        return convertView;
    }

    private static class ViewHolder {
        ImageView iconImageView; // public by def
        TextView temperatureLabel;
        TextView dayLabel;
    }
}
DailyForecastActivity.java
package james.stormy.UI;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;

import java.util.Arrays;

import james.stormy.Adapters.DayAdapter;
import james.stormy.R;
import james.stormy.weather.Day;

public class DailyForecastActivity extends ListActivity {
    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);
    }
}
MainActivity.java
package james.stormy.UI;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import james.stormy.R;
import james.stormy.weather.Current;
import james.stormy.weather.Day;
import james.stormy.weather.Hour;
import james.stormy.weather.forecast;


public class MainActivity extends ActionBarActivity {

    public static final String TAG = MainActivity.class.getSimpleName();
    public static final String DAILY_FORECAST = "DAILY_FORECAST";

    private forecast mForecast;
    @InjectView(R.id.time_label) TextView mTimeLabel;
    @InjectView(R.id.TempLabel) TextView mTemperatureLabel;
    @InjectView(R.id.hum_value) TextView mHumidityValue;
    @InjectView(R.id.PRECIP_VALUE) TextView mPrecipValue;
    @InjectView(R.id.summaryLabel) TextView mSummaryLabel;
    @InjectView(R.id.icon_image_view) ImageView mIconImageView;
    @InjectView(R.id.progressBar) ProgressBar mProgressBar;
    @InjectView(R.id.location_view) TextView mLocationLabel;
    @InjectView(R.id.refreshImageView) ImageView mRefreshButton;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);

        mProgressBar.setVisibility(View.INVISIBLE);



        final double latitude = 42.683333;
        final double longitude = -81.797222;

        mRefreshButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getForecast(latitude, longitude);
            }
        });

        getForecast(latitude,longitude);
        Log.d(TAG,"Main UI is running!");

    }



    private void getForecast(double lat, double Long) {
        mSummaryLabel.setText("Getting Current Weather...");
        String apiKey = "b964c75d003c3f8b1a737880cb6deabb";


        String forecastUrl = "https://api.forecast.io/forecast/"+apiKey+"/"+lat+","+Long;
        if (isNetworkAvailable()) {
            toggleRefresh();
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder().url(forecastUrl).build();


            Call call = client.newCall(request);
            call.enqueue(new Callback() {

                @Override
                  public void onFailure(Request request, IOException e)  {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            toggleRefresh();
                            AlertUserAboutError();

                        }
                    });


                }

                @Override
                public void onResponse(Response response) throws IOException {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            toggleRefresh();
                        }
                    });
                    try {
                        String jsonData = response.body().string();
                        Log.v(TAG, jsonData);

                        if (response.isSuccessful()) {
                            mForecast = parseForecastDetails(jsonData);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {

                                    updateDisplay();


                                }
                            });
                        } else {
                            AlertUserAboutError();


                        }
                    } catch (IOException e) {
                        Log.e(TAG, "Exception caught:", e);

                    }
                    catch (JSONException e ) {

                        Log.e(TAG, "Exception caught:", e);
                    }

                }


            });
        } else {

            mSummaryLabel.setText(getString(R.string.error_message_simplified));
            Toast.makeText(this, getString(R.string.network_unavailable_message), Toast.LENGTH_LONG).show();
        }
    }

    private void toggleRefresh() {
        if (mProgressBar.getVisibility()==View.VISIBLE) {
        mProgressBar.setVisibility(View.INVISIBLE);
        mRefreshButton.setVisibility(View.VISIBLE);
        }
        else {
            mProgressBar.setVisibility(View.VISIBLE);
            mRefreshButton.setVisibility(View.INVISIBLE);
        }
    }

    private void updateDisplay() {
        Current current = mForecast.getCurrent();
        mTemperatureLabel.setText(current.getTemp() + "");


        mTimeLabel.setText("At "+ current.getFormattedTime()+ " it will be");
        mHumidityValue.setText(current.getHum() + "");
        mPrecipValue.setText(current.getPrecipChance()+"%");
        mSummaryLabel.setText(current.getSummary());
        Drawable drawable = getResources().getDrawable(current.getIconId());
        mIconImageView.setImageDrawable(drawable);
        mLocationLabel.setText(current.getTimeZone());

    }

    private forecast parseForecastDetails(String jsonData) throws JSONException{
        forecast forecast = new forecast();

            forecast.setCurrent(getCurrentDetails(jsonData));
            forecast.setHourlyForecast(getHourlyForecast(jsonData));
            forecast.setDailyForcast(getDailyForecast(jsonData));

        return forecast;
    }

    private Day[] getDailyForecast(String jsonData) throws JSONException{
        JSONObject forecast = new JSONObject(jsonData);
        String timeZone = forecast.getString("timezone");
        JSONObject daily = forecast.getJSONObject("daily");
        JSONArray data = daily.getJSONArray("data");

        Day[] days = new Day[data.length()];
        for (int i = 0; i < data.length(); i++){
            JSONObject jsonDay = data.getJSONObject(i);
            Day day = new Day();

            day.setSummary(jsonDay.getString("summary"));
            day.setIcon(jsonDay.getString("icon"));
            day.setTempMax(jsonDay.getDouble("temperatureMax"));
            day.setTime(jsonDay.getLong("time"));
            day.setTimeZone(timeZone);

            days[i] = day;
        }

        return days;
    }

    private Hour[] getHourlyForecast(String jsonData) throws JSONException{
        JSONObject forecast = new JSONObject(jsonData);
        String timeZone = forecast.getString("timezone");
        JSONObject hourly = forecast.getJSONObject("hourly");
        JSONArray data = hourly.getJSONArray("data");

        Hour[] hours = new Hour[data.length()];

        for (int i = 0; i < data.length(); i++) {
            JSONObject jsonHour = data.getJSONObject(i);
            Hour hour = new Hour();
            hour.setSummary(jsonHour.getString("summary"));
            hour.setTemperature(jsonHour.getDouble("temperature"));
            hour.setIcon(jsonHour.getString("icon"));
            hour.setTime(jsonHour.getLong("time"));
            hour.setTimezone(timeZone);

            hours[i] = hour;
        }
        return hours;
    }

    private boolean isNetworkAvailable() {
        ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        boolean isAvailable = false;
        if (networkInfo != null && networkInfo.isConnected()) {
            isAvailable = true;

        }
        return isAvailable;
     }

    private void AlertUserAboutError() {
        mSummaryLabel.setText(getString(R.string.error_message_simplified));
        AlertDialogFragment dialog = new AlertDialogFragment();
        dialog.show(getFragmentManager(),"error_dialog");
    }
    private Current getCurrentDetails(String data) throws JSONException{
        JSONObject forecast = new JSONObject(data);
        String timeZone = forecast.getString("timezone");
        Log.i(TAG,"From JSON:" + timeZone);

        JSONObject currently = forecast.getJSONObject("currently");

        Current current = new Current();
        current.setHum(currently.getDouble("humidity"));
        current.setTime(currently.getLong("time"));
        current.setIcon(currently.getString("icon"));
        current.setPrecipChance(currently.getDouble("precipProbability"));
        current.setSummary(currently.getString("summary"));
        current.setTemp(currently.getDouble("temperature"));
        current.setTimeZone(timeZone);

        Log.d(TAG, current.getFormattedTime());

        return current;
    }

    @OnClick (R.id.dailyButton)
    public void startDailyActivity(View view) {
        Intent intent = new Intent(this, DailyForecastActivity.class);
        intent.putExtra(DAILY_FORECAST,mForecast.getDailyForcast());
        startActivity(intent);
    }


}
Current.java
package james.stormy.weather;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class Current {
    private String mIcon;
    private long mTime;
    private double mTemp;
    private double mHum;
    private double mPrecipChance;
    private String mSummary;

    public String getTimeZone() {
        return mTimeZone;
    }

    public void setTimeZone(String timeZone) {
        mTimeZone = timeZone;
    }

    private String mTimeZone;

    public String getIcon() {
        return mIcon;
    }

    public void setIcon(String icon) {
        mIcon = icon;
    }

    public long getTime() {
        return mTime;
    }

    public String getFormattedTime() {
        SimpleDateFormat formatter = new SimpleDateFormat("h:mm a");
        formatter.setTimeZone(TimeZone.getTimeZone(getTimeZone()));
        Date dateTime = new Date(getTime() * 1000);
        String timeString = formatter.format(dateTime);

        return timeString;
    }

    public void setTime(long time) {
        mTime = time;
    }

    public int getTemp() {
        return (int)Math.round(mTemp);
    }
    public int getIconId() {
        return forecast.getIconId(mIcon);
    }


    public void setTemp(double temp) {
        mTemp = temp;
    }

    public double getHum() {
        return mHum;
    }

    public void setHum(double hum) {
        mHum = hum;
    }

    public int getPrecipChance() {
        double precipPercent = mPrecipChance * 100;
        return (int)Math.round(precipPercent);
    }

    public void setPrecipChance(double precipChance) {
        mPrecipChance = precipChance;

    }

    public String getSummary() {
        return mSummary;
    }

    public void setSummary(String summary) {
        mSummary = summary;
    }



}
Day.java
package james.stormy.weather;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class Current {
    private String mIcon;
    private long mTime;
    private double mTemp;
    private double mHum;
    private double mPrecipChance;
    private String mSummary;

    public String getTimeZone() {
        return mTimeZone;
    }

    public void setTimeZone(String timeZone) {
        mTimeZone = timeZone;
    }

    private String mTimeZone;

    public String getIcon() {
        return mIcon;
    }

    public void setIcon(String icon) {
        mIcon = icon;
    }

    public long getTime() {
        return mTime;
    }

    public String getFormattedTime() {
        SimpleDateFormat formatter = new SimpleDateFormat("h:mm a");
        formatter.setTimeZone(TimeZone.getTimeZone(getTimeZone()));
        Date dateTime = new Date(getTime() * 1000);
        String timeString = formatter.format(dateTime);

        return timeString;
    }

    public void setTime(long time) {
        mTime = time;
    }

    public int getTemp() {
        return (int)Math.round(mTemp);
    }
    public int getIconId() {
        return forecast.getIconId(mIcon);
    }


    public void setTemp(double temp) {
        mTemp = temp;
    }

    public double getHum() {
        return mHum;
    }

    public void setHum(double hum) {
        mHum = hum;
    }

    public int getPrecipChance() {
        double precipPercent = mPrecipChance * 100;
        return (int)Math.round(precipPercent);
    }

    public void setPrecipChance(double precipChance) {
        mPrecipChance = precipChance;

    }

    public String getSummary() {
        return mSummary;
    }

    public void setSummary(String summary) {
        mSummary = summary;
    }



}
forecast.java
package james.stormy.weather;

import android.os.Parcel;
import android.os.Parcelable;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class Day implements Parcelable{
    private long mTime;
    private String mSummary;
    private double mTempMax;
    private String mIcon;
    private String mTimeZone;

    public long getTime() {
        return mTime;
    }

    public void setTime(long time) {
        mTime = time;
    }

    public String getSummary() {
        return mSummary;
    }

    public void setSummary(String summary) {
        mSummary = summary;
    }

    public int getTempMax() {
        return (int) Math.round(mTempMax);
    }

    public void setTempMax(double tempMax) {
        mTempMax = tempMax;
    }

    public String getIcon() {
        return mIcon;
    }

    public void setIcon(String icon) {
        mIcon = icon;
    }

    public String getTimeZone() {
        return mTimeZone;
    }

    public void setTimeZone(String timeZone) {
        mTimeZone = timeZone;
    }
    public int getIconId() {
        return Forecast.getIconId(mIcon);

    }
    public String getDayWeek() {
        SimpleDateFormat formatter = new SimpleDateFormat("EEEE");
        formatter.setTimeZone(TimeZone.getTimeZone(mTimeZone));
        Date dateTime = new Date(mTime * 1000);
        return formatter.format(dateTime);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(mTime);
        dest.writeString(mSummary);
        dest.writeDouble(mTempMax);
        dest.writeString(mIcon);
        dest.writeString(mTimeZone);
    }
    private Day(Parcel in) {
        mTime = in.readLong();
        mSummary = in.readString();
        mTempMax = 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];
        }
    };

}

If it Matters, I am Testing This App On A Samsung Galaxy s4 mini. Also, The App Will Crash If MY Wi-Fi Isn't on. I think I will make the hourly and 7 day buttons disappear if the Wi-Fi is off.

3 Answers

Hello,

You forgot to use setListAdapter.

public class DailyForecastActivity extends ListActivity {
    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);    // <--- Add this
    }
}

That should get you data when you hit the 7 days button. Let me know if you have other issues.

Joe Goodall
Joe Goodall
3,483 Points

After adding setListAdapter(adapter); and running the code I get this error:

04-29 17:08:06.367 16614-16614/com.getstrength.stormy E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.getstrength.stormy, PID: 16614 java.lang.NullPointerException: id == null at java.util.TimeZone.getTimeZone(TimeZone.java:349) at com.getstrength.stormy.weather.Day.getDayOfTheWeek(Day.java:66) at com.getstrength.stormy.adapters.DayAdapter.getView(DayAdapter.java:65) at android.widget.AbsListView.obtainView(AbsListView.java:2344) at android.widget.ListView.makeAndAddView(ListView.java:1864) at android.widget.ListView.fillDown(ListView.java:698) at android.widget.ListView.fillFromTop(ListView.java:759) at android.widget.ListView.layoutChildren(ListView.java:1673) at android.widget.AbsListView.onLayout(AbsListView.java:2148) at android.view.View.layout(View.java:15596) at android.view.ViewGroup.layout(ViewGroup.java:4966) at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076) at android.view.View.layout(View.java:15596) at android.view.ViewGroup.layout(ViewGroup.java:4966) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573) at android.widget.FrameLayout.onLayout(FrameLayout.java:508) at android.view.View.layout(View.java:15596) at android.view.ViewGroup.layout(ViewGroup.java:4966) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557) at android.widget.LinearLayout.onLayout(LinearLayout.java:1466) at android.view.View.layout(View.java:15596) at android.view.ViewGroup.layout(ViewGroup.java:4966) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573) at android.widget.FrameLayout.onLayout(FrameLayout.java:508) at android.view.View.layout(View.java:15596) at android.view.ViewGroup.layout(ViewGroup.java:4966) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2072) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1829) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767) at android.view.Choreographer.doCallbacks(Choreographer.java:580) at android.view.Choreographer.doFrame(Choreographer.java:550) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5239) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

Hello,

Since your problem is different than that of the start of the topic, could you create a new topic here on the forums, include the full error code again, and include a copy of your java files?

Joe Goodall
Joe Goodall
3,483 Points

Thanks for replying so quickly.

I'll start a new topic.

Whoa! I had the same issue and the same solution helped me... Surprisingly spooky.