So what I am trying to do is pass ObjectB from the first activity to the second. ObjectB contains another object, ObjectA. I am getting really confused as to how to implement this. Also, I am using a list in ObjectA because I am going to have multiple integer values instead of just the 1 in this example. Here is the first activity:
private ObjectB objB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
objB = new ObjectB();
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("KEY", objB);
startActivity(intent);
}
and the second activity:
private ObjectB objB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
objB = new ObjectB();
TextView tv = (TextView) findViewById(R.id.TextView1);
Bundle b = getIntent().getExtras();
objB = b.getParcelable("KEY");
tv.setText(objB.obj.stage1Count);
}
Class for ObjectB:
public class ObjectB implements Parcelable{
public ObjectA obj;
public ObjectB() { obj = new ObjectA(); }
public ObjectA getObj() {
return obj;
}
public ObjectB(Parcel in) {
readFromParcel(in);
}
public void setObj(ObjectA obj) {
this.obj = obj;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelable(obj, flags);
}
private void readFromParcel(Parcel in) {
obj = in.readParcelable(ObjectA.class.getClassLoader());
}
public static final Parcelable.Creator CREATOR =
new Parcelable.Creator() {
public ObjectB createFromParcel(Parcel in) {
return new ObjectB(in);
}
public ObjectB[] newArray(int size) {
return new ObjectB[size];
}
};
}
Class for ObjectA:
public class ObjectA implements Parcelable{
public int stage1Count = 1;
public ObjectA() { ; };
public ObjectA(Parcel in) {
readFromParcel(in);
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
List<Integer> iList = new ArrayList<Integer>();
iList.add(stage1Count);
dest.writeList(iList);
}
private void readFromParcel(Parcel in) {
List<Integer> iList = new ArrayList<Integer>();
iList = in.readArrayList(Integer.class.getClassLoader());
stage1Count = iList.get(0);
}
public static final Parcelable.Creator CREATOR =
new Parcelable.Creator() {
public ObjectA createFromParcel(Parcel in) {
return new ObjectA(in);
}
public ObjectA[] newArray(int size) {
return new ObjectA[size];
}
};
}
Try this in first activity:
List<ObjectB> list = new ArrayList<ObjectB>;
intent.putParcelableArrayListExtra("key", list);
And in second activity
List<ObjectB> list = new ArrayList<ObjectB>;
list = bundle.getParcelableArrayList("key");
From list get your objectB
This was a really dumb question but I figured it out after a really long time haha. The problem is tv.setText(objB.obj.stage1Count);
because objB.obj.stage1Count is actually an integer but for some reason it does not ring any bells when you try to use it as a string, idk maybe its something with the parceing.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.