我在自定义arraylist中有一个对象作为“finaljsoncontent”,现在我试图将这个“finaljsoncontent”数组传递给另一个Activity,我也尝试了getter和setter,还捆绑了,但我不能帮助我该怎么做 提前谢谢。
答案 0 :(得分:0)
您可以尝试实施Parcelable
,然后您可以将其传递到一个包中。您需要将对象缩减为大多数基本类型才能执行此操作。否则,您可以扩展Application
类并将其存储在那里。您可以使用getApplicationContext()
调用来检索它。或者,当然,您总是可以创建某种类型的静态全局类,所有类都可以引用它。
这是我的parcelable实现之一..
package warrior.mail.namespace;
import android.os.Parcel;
import android.os.Parcelable;
public class JView implements Parcelable {
public String subject;
public String from;
public boolean unread;
public String body;
public int inboxIndex;
private long id;
public static final Parcelable.Creator<JView> CREATOR = new Parcelable.Creator<JView>() {
public JView createFromParcel(Parcel in) {
return new JView(in);
}
public JView[] newArray(int size) {
return new JView[size];
}
};
public JView(){
body = "";
}
public JView(String subject,String from,boolean unread){
body = "";
this.subject = subject;
this.from = from;
this.unread = unread;
}
public JView(Parcel parcel){
subject = parcel.readString();
from = parcel.readString();
body = parcel.readString();
unread = parcel.createBooleanArray()[0];
inboxIndex = parcel.readInt();
}
@Override
public int describeContents() {
return inboxIndex;
}
@Override
public void writeToParcel(Parcel out, int arg1) {
out.writeString(subject);
out.writeString(from);
out.writeString(body);
boolean[] array = new boolean[] {unread};
out.writeBooleanArray(array);
out.writeInt(inboxIndex);
}
public void setIndex(int index){
inboxIndex = index;
}
public void setUnread(boolean arg){
unread = arg;
}
public void setContent(String content){
body = content;
}
public void setSubject(String subject){
this.subject = subject;
}
public void setFrom(String f){
from = f;
}
public void setId(long arg){
id = arg;
}
public long getId(){
return id;
}
public void updateIndex(){
}
}
答案 1 :(得分:0)
检查出来:How do I pass an object from one activity to another on Android?
您的类“JSonKey”应该实现parcealable或serializable,以便Android可以将它从活动“发送”到另一个活动。
答案 2 :(得分:0)
你可以创建你的类Parcelable
(特定于android)或者使它像java一样可序列化(只需用你的类编写实现Serializable)