Reputation: 385
I have a problem with passing my 2D string array from one activity to another activity I tried some codes...but they show some errors
My string array is:
String[][] commuterDetails=new String[2][5];
commuterDetails=
{
{ "a", "b","c", "d","e" },
{"f", "g","h", "i","j" }
};
And I tried some codes
In first Activity
Intent summaryIntent = new Intent(this, Second.class);
Bundle b=new Bundle();
b.putSerializable("Array", commuterDetails);
summaryIntent.putExtras(b);
startActivity(summaryIntent);
In second activity
Bundle b = getIntent().getExtras();
String[][] list_array = (String[][])b.getSerializable("Array");
But it showing error
Caused by: java.lang.ClassCastException: [Ljava.lang.Object;
I am new in android, please help me
Upvotes: 3
Views: 955
Reputation: 5515
You may define a custom class which implements Parcelable
and contains logic to read and write 2-dimensional-array from/to Parcel. Afterwards, put that parcelable object inside Bundle for transportation.
UPDATE
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];
}
};
}
Upvotes: 1
Reputation: 1525
make your commuterDetails static and access in other activity like this
FirstActivity.commuterDetails[][]
Upvotes: 0