Pass 2D Array to Another Activity

passing 2d array to a new activity in android

You have to use:

 String [][]str;
Intent summaryIntent = new Intent(this, Second.class);
Bundle b=new Bundle();
b.putSerializable("Array", str);
summaryIntent.putExtras(b);
startActivity(summaryIntent);

For Recieving the Array use:

Bundle b = getIntent().getExtras();
String[][] list_array = (String[][])b.getSerializable("Array");

Thanks

How to pass multidimensional Arraylist to another activity using bundle

You can use Gson library for this, no need to implement serializable.

Suppose your arraylist is :

ArrayList<ArrayList<String>> bigArrayList= new ArrayList<>();

After that, you can add it to the intent as below :

Intent i = new Intent(Activity1.this, Activity2.class);
i.putExtra("data", new Gson().toJson(bigArrayList));

You can later retrieve this in Activity2 as below :

String extra = getIntent().getStringExtra("data");
ArrayList<ArrayList<String>> bigArrayList= new Gson().fromJson(extra, new TypeToken<ArrayList<ArrayList<String>>>(){}.getType());

It works for me! I hope It helps you!

How to pass a 2D array of double between activities?

Convert double 2D array to String 2D array and send it just like you did :

To send :

b.putSerializable("array", other_locations_string);

To get :

String[][] value = (String[][]) b.getSerializable("array");

And then again convert it into double 2d array.

The reason for this behaviour is because Java (and consequently Android) does not let you cast Objects to primitive types, or arrays of primitive types. Why? Because Java considers a valid cast one which converts a super-class object to a sub-class object or vice versa. String[][] extends Object, while double and double[][] do not.



Related Topics



Leave a reply



Submit