Home > database >  How do i pass objects from 1 class to another?
How do i pass objects from 1 class to another?

Time:06-20

I have 3 classes: MainActivity, homePage and createPage; and a list List<Recipe> recipeList = new ArrayList<>() in MainActivity.

The user enters the homePage from the MainActivity. From homePage, the user can enter createPage and create a new recipe. This new recipe is intended to be passed back to MainActivity.

I've searched online and came across parcels. But when I tried, I get a NullPointerException.

Code for createPage where the list is passed on

ArrayList<Recipe> rList = new ArrayList<>();
Recipe r = new Recipe(...);
rList.add(r)
Intent i = new Intent();
Bundle b = new Bundle();
b.putParcelableArrayList("recipe", (ArrayList<? extends Parcelable>) rList);
i.putExtras(b);
i.setClass(createPage.this, homePage.class);
startActivity(i);

Code for homePage where the list is received.

Is there something wrong with the getIntent()? Because when moving from MainActivity to homePage, it doesn't receive a bundle. Is this causing the error?

Intent intent = getIntent();
Bundle b = this.getIntent().getExtras();
if (b != null) {
    Recipe r = b.getParcelable("recipe");
    recipeList.add(r);
}

Code for Recipe class

public class Recipe implements Parcelable {

private String name;
private String description;
private String ingredients;
private int duration;
private String steps;
private int thumbnail;

protected Recipe(Parcel in) {
    name = in.readString();
    description = in.readString();
    ingredients = in.readString();
    duration = in.readInt();
    steps = in.readString();
    thumbnail = in.readInt();
}

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

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

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

public String getDescription() {
    return description;
}
public void setDescription(String description) {
    this.description = description;
}

public String getIngredients() {
    return ingredients;
}
public void setIngredients(String ingredients) {
    this.ingredients = ingredients;
}

public int getDuration() {
    return duration;
}
public void setDuration(int duration) {
    this.duration = duration;
}

public String getSteps() { return steps; }
public void setSteps(String steps) { this.steps = steps; }

public int getThumbnail() { return thumbnail; }

public Recipe() {}

public Recipe(String name, int duration, String ingredients, String description, String steps, int thumbnail) {
    this.name = name;
    this.description = description;
    this.ingredients = ingredients;
    this.duration = duration;
    this.steps = steps;
    this.thumbnail = thumbnail;
}

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

@Override
public void writeToParcel(Parcel parcel, int i) {
    parcel.writeString(name);
    parcel.writeString(description);
    parcel.writeString(ingredients);
    parcel.writeInt(duration);
    parcel.writeString(steps);
    parcel.writeInt(thumbnail);
}

}

CodePudding user response:

you are writing into Parcelable whole array under "recipe" key

b.putParcelableArrayList("recipe", (ArrayList<? extends Parcelable>) rList);

but on onther side you are looking not for list but for single Recipe item under same key

Recipe r = b.getParcelable("recipe");

you should use getParcelableArrayList or if you have only one Recipe for passing to another Activity just use putParcelable (not list)

CodePudding user response:

Alternatively you can use serializable, that will be less complex.

For reference : https://stackoverflow.com/a/2736612/9502601

Eventhough parcellables are more faster but if you want a less complex solution then you can go for it.

For Comparison between Serializable and Parcelable. https://stackoverflow.com/a/23647471/9502601

CodePudding user response:

You can use this gson Lib for this

implementation 'com.google.code.gson:gson:2.8.9'

Send Data with Intent

Recipe r = new Recipe(...);
String recipeString = new Gson().toJson(r);
intent.putExtra("recipe",recipeString);

// For ArrayList
ArrayList<Recipe> recipeList = new ArrayList<>();
String recipeString = new Gson().toJson(recipeList);
intent.putExtra("recipeList",recipeString);

Receive Data From Intent

Recipe r = new Gson().fromJson(intent.getStringExtra("recipe"), Recipe.class);

// For Array List
Type listType = new TypeToken<ArrayList<Recipe>>(){}.getType();
ArrayList<Recipe> recipeList = new Gson().fromJson(intent.getStringExtra("recipeList"),listType);
  • Related