Ana*_*and 1 android multidimensional-array android-activity
我是Android新手.请告诉我如何从一个Activity到另一个Activity获取二维字符串数组.谢谢
您可以使用Android ParcelableClass传递Serializable数据Arrays.以下是您案例中的示例.
public class MyParcelable implements Parcelable{
public String[][] strings;
public String[][] getStrings() {
return strings;
}
public void setStrings(String[][] strings) {
this.strings = strings;
}
public MyParcelable() {
strings = new String[1][1];
}
public MyParcelable(Parcel in) {
strings = (String[][]) in.readSerializable();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeSerializable(strings);
}
public static final Parcelable.Creator<MyParcelable> CREATOR = new Parcelable.Creator<MyParcelable>() {
@Override
public MyParcelable createFromParcel(Parcel in) {
return new MyParcelable(in);
}
@Override
public MyParcelable[] newArray(int size) {
return new MyParcelable[size];
}
};
}
Run Code Online (Sandbox Code Playgroud)
传递到另一个活动 -
public String[][] strings = new String[1][1];
strings[0][0] = "data";
MyParcelable myParcelable = new MyParcelable();
myParcelable.setStrings(strings);
intent.putExtra("parcel",myParcelable);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
要检索 -
Intent intent = getIntent();
Bundle b = intent.getExtras();
MyParcelable myParcelable = b.getParcelable("parcel");
strings = myParcelable.getStrings();
Log.d("Your String[0][0] is - ",strings[0][0]+"");
Run Code Online (Sandbox Code Playgroud)
输出 -
12-29 12:49:39.016: D/Your String[0][0] is -(1484): data
Run Code Online (Sandbox Code Playgroud)