[英]How do i pass objects from 1 class to another?
我有 3 个类: MainActivity
、 homePage
和createPage
; 和MainActivity
中的列表List<Recipe> recipeList = new ArrayList<>()
。
用户从homePage
进入MainActivity
。 从homePage
,用户可以进入createPage
并创建一个新配方。 这个新配方旨在传递回MainActivity
。
我在网上搜索了一下,发现有包裹。 但是当我尝试时,我得到了 NullPointerException。
传递列表的createPage
的代码
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);
接收列表的homePage
的代码。
getIntent()
有什么问题吗? 因为从MainActivity
移动到homePage
时,它没有收到捆绑包。 这是导致错误吗?
Intent intent = getIntent();
Bundle b = this.getIntent().getExtras();
if (b != null) {
Recipe r = b.getParcelable("recipe");
recipeList.add(r);
}
Recipe
类的代码
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);
}
}
或者,您可以使用可序列化,这将不那么复杂。
供参考: https ://stackoverflow.com/a/2736612/9502601
尽管 Parcellables 更快,但如果您想要一个不太复杂的解决方案,那么您可以选择它。
用于 Serializable 和 Parcelable 之间的比较。 https://stackoverflow.com/a/23647471/9502601
您正在"recipe"
键下写入Parcelable
整个数组
b.putParcelableArrayList("recipe", (ArrayList<? extends Parcelable>) rList);
但另一方面,您不是在寻找列表,而是在同一键下寻找单个Recipe
项目
Recipe r = b.getParcelable("recipe");
您应该使用getParcelableArrayList
或者如果您只有一个用于传递给另一个Activity
的Recipe
,只需使用putParcelable
(不是列表)
您可以为此使用这个 gson Lib
implementation 'com.google.code.gson:gson:2.8.9'
有意图地发送数据
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);
从 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.