John
John

Reputation: 1888

Passing an object from one android activity to another

I have a class that I'm using for parsing data from a csv file (called CSVReader), inside of that there is some methods for the parsing as well as a couple of strings and a list that is of a custom object type (List allRecords). What I'm doing is when the app loads, parsing all the data into that list and then trying to pass that information along to the next activity but inside the next activity I keep getting allRecords as being null.

LoginActivity

    CSVReader data = new CSVReader();
    data.populateRecords(this);
    Intent intent = new Intent(LoginActivity.this, Find.class);
    Bundle bundle = new Bundle();
    bundle.putParcelable("list", data);
    intent.putExtra("bundle", bundle);
    startActivity(intent);

I've gone through the debugger and the bundle definitely has the data in there.

Find

Intent intent = getIntent();
Bundle bundle = intent.getBundleExtra("bundle");
data = (CSVReader) bundle.getParcelable("list");

Using the debugger still, and mMap (in the bundle) is now null and so is data.

Am I doing something wrong? Both classes DummyData and CSVReader implement Parcelable.

EDIT: Adding custom class CSVReader:

    List<DummyData> allRecords;
    private String base;
    private String location;
    private String partner;

    public static Creator<CSVReader> CREATOR = new Creator<CSVReader>(){
        @Override
        public CSVReader createFromParcel(Parcel source){
            return new CSVReader(source);
        }
        @Override
        public CSVReader[] newArray(int size) {
            return new CSVReader[size];
        }
    };

    private CSVReader(Parcel in){
            allRecords = new ArrayList<DummyData>();
            in.readTypedList(allRecords, DummyData.CREATOR);
            base = in.readString();
            location = in.readString();
            partner = in.readString();
    }
   ...
   ...

    @Override
    public int describeContents() {
            return 0;
    }
    @Override
    public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(base);
            dest.writeString(location);
            dest.writeString(partner);
            dest.writeList(allRecords);
     }

Upvotes: 0

Views: 77

Answers (3)

Andrew Senner
Andrew Senner

Reputation: 2509

[EDIT]

I was looking over your code again, and noticed you're writing the List to the parcel LAST and reading the list from the parcel FIRST. You have to read items from the parcel in the order they are written. Try putting dest.writeList(allRecords) at the top of the method so it is the first item written, or you can put in.readList(allRecords, DummyData.class.getClassLoader()) at the bottom of the list in that method.

Give it a shot.

//////////////////////////////////////////////

From the docs,

public final void readTypedList (List<T> list, Creator<T> c)

Read into the given List items containing a particular object type that were written with writeTypedList(List) at the current dataPosition(). The list must have previously been written via writeTypedList(List) with the same object type.

You're using writeList() to write the data to the parcel. Try using writeTypedList(). You could also try changing readTypedList() with readList() I believe. Something like:

in.readList(allRecords, CSVReader.class.getClassLoader());

Hope this helps.

Upvotes: 1

display name
display name

Reputation: 899

If you plan to pass data to another activity, you need to use objects of classes that implement Parcelable. Here is an example,

package com.weather.model;

import java.util.ArrayList;
import java.util.List;

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

public class Forcast implements Parcelable {

private String id;
private String code;
private String message;
private City city;
private String count;
private List<Weather> weatherList = new ArrayList<Weather>();

public Forcast(){
}

public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public String getCode() {
    return code;
}
public void setCode(String code) {
    this.code = code;
}
public String getMessage() {
    return message;
}
public void setMessage(String message) {
    this.message = message;
}
public City getCity() {
    return city;
}
public void setCity(City city) {
    this.city = city;
}
public String getCount() {
    return count;
}
public void setCount(String count) {
    this.count = count;
}
public List<Weather> getWeatherList() {
    return weatherList;
}
public void setWeatherList(List<Weather> weather) {
    this.weatherList = weather;
}


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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(id);
    dest.writeString(code);
    dest.writeString(message);
    dest.writeParcelable(city, flags);
    dest.writeString(count);
    dest.writeList(weatherList);

}

public static final Parcelable.Creator<Forcast> CREATOR = new Parcelable.Creator<Forcast>() {

    @Override
    public Forcast createFromParcel(Parcel source) {
        return new Forcast(source);
    }

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

protected Forcast(Parcel in){
    id = in.readString();
    code = in.readString();
    message = in.readString();
    city = (City)in.readParcelable(City.class.getClassLoader());
    count = in.readString();
    in.readList(weatherList, Weather.class.getClassLoader());
}

}

I included City model so you can see how it goes for an object in comparison to list of objects. Assume you have a Forcast instance with all the values as 'forcast' ( Forcast forcast = new Forcast() or something similar)

Intent intent = new Intent(this, SomeActivity.class);
intent.putExtra(com.weather.model,forcast)
startActivity(intent)

I hope that helps

Upvotes: 1

Efe Budak
Efe Budak

Reputation: 659

You need to put bundle object by using putExtras method. Then you can get the bundle by using getIntent().getExtras().

Intent intent = new Intent(LoginActivity.this, Find.class);
Bundle bundle = new Bundle();
bundle.putParcelable("list", data);
intent.putExtras( bundle);
startActivity(intent);

Intent intent = getIntent();
Bundle bundle = intent.getExtras();
data = (CSVReader) bundle.getParcelable("list");

Upvotes: 0

Related Questions