我知道在這個主題上有很多問題,但我無法找到針對特定情況的答案。基本上我有一個相當簡單的類的二維數組,我需要通過。我試圖用Parcelable
,我跑我的課通過this得到這個:在活動之間傳遞2D對象
import android.os.Parcel;
import android.os.Parcelable;
public class Notam implements Parcelable {
public String airfield;
public String identifier;
public String notamText;
public String fromTime;
public String untilTime;
public boolean hidden;
// one constructor
public Notam(String airfieldname, String newIdentifier, String newNotamText, String newFrom, String newUntil) {
airfield = airfieldname;
identifier = newIdentifier;
notamText = newNotamText;
fromTime = newFrom;
untilTime = newUntil;
}
public void setHidden(boolean changeHidden) {
hidden = changeHidden;
}
protected Notam(Parcel in) {
airfield = in.readString();
identifier = in.readString();
notamText = in.readString();
fromTime = in.readString();
untilTime = in.readString();
hidden = in.readByte() != 0x00;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(airfield);
dest.writeString(identifier);
dest.writeString(notamText);
dest.writeString(fromTime);
dest.writeString(untilTime);
dest.writeByte((byte) (hidden ? 0x01 : 0x00));
}
@SuppressWarnings("unused")
public static final Parcelable.Creator<Notam> CREATOR = new Parcelable.Creator<Notam>() {
@Override
public Notam createFromParcel(Parcel in) {
return new Notam(in);
}
@Override
public Notam[] newArray(int size) {
return new Notam[size];
}
};
}
我已經試過的事情,我在這裏找到所有的方式,但我現在丟失。在主要活動的那一刻我有
Notam[][] notamList = new Notam[10][100];
Intent intent = new Intent(this, NotamList.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("notamList", notamList);
intent.putExtras(bundle);
startActivity(intent);
然後在接收活動:
Notam[][] notamList = new Notam[10][100];
Bundle data = getIntent().getExtras();
notamList = data.getParcelableArrayList("notamList");
但是,這不是甚至接近工作,所以我問你的智慧和指導!
請說明「甚至不接近工作」的含義。 – Egor
嗯,我在主活動中出現這個錯誤「Bundle類型中的方法putParcelableArrayList(String,ArrayList <?extends Parcelable>)不適用於參數(String,Notam [] [])」和接收活動「Multiple在這條線 \t標記 - 類型不匹配:不能從NOTAM轉換爲NOTAM [] \t - 類型不匹配:不能從ArrayList中轉換爲NOTAM [] []」 –
Gavin