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

The list is empty or (not displaying any information), and the string "There is no display" is the only thing displayed.

I am unable to make the daily list include the daily data. I have also tried to debug and found that when calling "intent.putExtra(DAILY_FORECAST, mForecast.getDailyForcast());" the getDailyForcast returns information and is not null.

From the layout perspective, I have tried to add constraints to the ListView inside activity_daily_forecast layout, still not helping.

Can someone help please?

package stormy.abdullah.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.support.v7.app.AppCompatActivity; import android.os.Bundle; 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 org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject;

import java.io.IOException;

import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import stormy.abdullah.stormy.R; import stormy.abdullah.stormy.weather.Current; import stormy.abdullah.stormy.weather.Day; import stormy.abdullah.stormy.weather.Forecast; import stormy.abdullah.stormy.weather.Hour;

public class MainActivity extends AppCompatActivity {

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



@BindView(R.id.timeLabel) TextView mTimeLabel;
@BindView(R.id.temperatureLabel) TextView mTemperatureLabel;
@BindView(R.id.humidityValue) TextView mHumidityValue;
@BindView(R.id.precipValue) TextView mPrecipValue;
@BindView(R.id.summaryLabel) TextView mSummaryLabel;
@BindView(R.id.iconImageView) ImageView mIconImageView;
@BindView(R.id.refreshImageView) ImageView mRefreshImageView;
@BindView(R.id.progressBar) ProgressBar mProgressBar;

//@BindView(R.id.humidityLabel) TextView mHumidityLabel;

//@InjectView(R.id.timeLabel) TextView mTimeLabel;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);
    mProgressBar.setVisibility(View.INVISIBLE);

    final double latitude = 24.7136;
    final double longitude =46.6753;
    mRefreshImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            getForcast(latitude, longitude);
        }
    });
    getForcast(latitude, longitude);

    Log.d(TAG, "Main UI code is running!");


}

private void getForcast(double latitude, double longitude) {
    String apiKey = "580374c86b4a9c379ab5e8da9cc6f41d";

// 24.7136; // 46.6753;

    String forecastURL = "https://api.darksky.net/forecast/" + apiKey + "/" + latitude + "," + longitude;
    Log.i(TAG, "APIURL: " + forecastURL);

    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(Call call, IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        toggleRefresh();
                    }
                });
                alertUserAboutError();
            }

            @Override
            public void onResponse(Call call, 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 = parseForcastDetails(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 {
        Toast.makeText(this, R.string.network_unavailable_message, Toast.LENGTH_LONG).show();
    }
}

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

}

private void updateDisplay() {
    Current current = mForecast.getCurrent();
    mTemperatureLabel.setText(current.getTemprature() +"");
    mTimeLabel.setText("At " + current.getFormattedTime() + " it will be");
    //mHumidityLabel.setText(mCurrent.getHumidity() + "");
    mHumidityValue.setText(current.getHumidity() + "");
    mPrecipValue.setText(current.getPrecipChance() + "%");
    mSummaryLabel.setText(mForecast.getCurrent().getSummary());
    Drawable drawable = getResources().getDrawable(mForecast.getCurrent().getIconID());

    mIconImageView.setImageDrawable(drawable);
}

private Forecast parseForcastDetails(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.setTemperatureMax(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++){
        Hour hour = new Hour();
        JSONObject jsonHour = data.getJSONObject(i);


        hour.setIcon(jsonHour.getString("icon"));
        hour.setSummary(jsonHour.getString("summary"));
        hour.setTemprature(jsonHour.getDouble("temperature"));
        hour.setTime(jsonHour.getLong("time"));
        hour.setTimezone(timezone);

        hours[i] = hour;
    }


    return hours;
}

private Current getCurrentDetails(String jsonData) throws JSONException{
    JSONObject forecast = new JSONObject(jsonData);
    String timezone = forecast.getString("timezone");
    Log.i(TAG, "From JSON: " + timezone);

    JSONObject currently = forecast.getJSONObject("currently");
    //Log.i(TAG, "From JSON Currently: " + currently);

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


    return current;
}

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() {
    AlertDialogFraggment dialog = new AlertDialogFraggment();
    dialog.show(getFragmentManager(), "error_dialog");
}

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

}


package stormy.abdullah.stormy.ui;

import android.app.ListActivity; import android.content.Intent; import android.os.Parcelable; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.ArrayAdapter;

import java.util.Arrays;

import stormy.abdullah.stormy.R; import stormy.abdullah.stormy.adapters.DayAdapter; import stormy.abdullah.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);
    if(adapter.isEmpty()){
        Log.i(this.getClass().getSimpleName(), "YES ADAPTOR IS EMPTY");
    }else{
        Log.i(this.getClass().getSimpleName(), "NO ADAPTOR IS NOT EMPTY");
    }
    //setListAdapter(adapter);



}

}


package stormy.abdullah.stormy.weather;

import stormy.abdullah.stormy.R;

/**

  • Created by User on 31/03/2018. */

public class Forecast { private Current mCurrent; private Hour[] mHourlyForecast; private Day[] mDailyForcast;

public Current getCurrent() {
    return mCurrent;
}

public void setCurrent(Current current) {
    mCurrent = current;
}

public Hour[] getHourlyForecast() {
    return mHourlyForecast;
}

public void setHourlyForecast(Hour[] hourlyForecast) {
    mHourlyForecast = hourlyForecast;
}

public Day[] getDailyForcast() {
    return mDailyForcast;
}

public void setDailyForcast(Day[] dailyForcast) {
    mDailyForcast = dailyForcast;
}

public static int getIconID(String iconString){

    int iconID = R.drawable.cloudy;
    //iconString = "cloudy";
    if (iconString.equals("clear-day")) {
        iconID = R.drawable.clear_day;
    }
    else if (iconString.equals("clear-night")) {
        iconID = R.drawable.clear_night;
    }
    else if (iconString.equals("rain")) {
        iconID = R.drawable.rain;
    }
    else if (iconString.equals("snow")) {
        iconID = R.drawable.snow;
    }
    else if (iconString.equals("sleet")) {
        iconID = R.drawable.sleet;
    }
    else if (iconString.equals("wind")) {
        iconID = R.drawable.wind;
    }
    else if (iconString.equals("fog")) {
        iconID = R.drawable.fog;
    }
    else if (iconString.equals("cloudy")) {
        iconID = R.drawable.cloudy;
    }
    else if (iconString.equals("partly-cloudy-day")) {
        iconID = R.drawable.partly_cloudy;
    }
    else if (iconString.equals("partly-cloudy-night")) {
        iconID = R.drawable.cloudy_night;
    }

    return iconID;
}

}


package stormy.abdullah.stormy.weather;

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

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

import javax.xml.transform.Source;

/**

  • Created by User on 31/03/2018. */

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

public Day() {
}

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 getTemperatureMax() {
    return (int)Math.round(mTemperatureMax);
}

public void setTemperatureMax(double temperatureMax) {
    mTemperatureMax = temperatureMax;
}

public String getIcon() {
    return mIcon;
}

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

public String getTimeZone() {
    return mTimeZone;
}

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

private String mTimeZone;
public int getIconId(){
    return Forecast.getIconID(mIcon);
}

public String getDayOfTheWeek() {
    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(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 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];
    }
};

}

1 Answer

Maximilian Hertelendy
Maximilian Hertelendy
18,148 Points

I had the same issue. I forgot these changes in the DayAdapter

@Override
public int getCount() {
    return mDays.length;  // <---Check this
}

@Override
public Object getItem(int position) {
    return mDays[position]; // <---Check this
}