How to pass a 2D array twice between actions?

I need to pass a two-dimensional double-action array, this is what I wrote so far: In the first action (sender):

Intent intent =new Intent(MapsActivity.this, RoutePath.class); Bundle b = new Bundle(); b.putSerializable("other_locations", other_locations); intent.putExtras(b); startActivity(intent); 

In the second (receiver):

 Intent intent = getIntent(); Bundle b = intent.getExtras(); double[][] other_locations=(double[][]) b.getSerializable("other_locations"); 

But I got a ClassCastException, am I something wrong?

+4
source share
2 answers

Convert a double 2D array to a String 2D array and send it the same way you do:

Submit:

 b.putSerializable("array", other_locations_string); 

To obtain:

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

And then converts it back to a 2d double array .

The reason for this behavior is that Java (and therefore Android) does not allow you to inject objects into primitive types or arrays of primitive types. What for? Because Java considers valid a listing that converts a superclass object to a subclass object, or vice versa. String[][] extends Object , but double and double[][] - no.

+4
source

try it

Convert 2D array to Hashmap and then use it

 Map<Double, Double> map = new HashMap<Double, Double>(other_locations.length); for (Double[] mapping : other_locations) { map.put(mapping[0], mapping[1]); } 

And install the card as an additional plan and get an additional one in the second action

+1
source

All Articles