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

I need to pass a 2D array of double between activities,this is what i wrote so far :
In the first activity (the 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 one (the receiver ):
Intent intent = getIntent();
Bundle b = intent.getExtras();
double[][] other_locations=(double[][]) b.getSerializable("other_locations");
But I got the ClassCastException, am I doing something wrong?

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.

Try this
Convert 2D Array into 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 set map as intent extra and get the extra in second activity

Related

Sending a double data value from one application to another application in android

I want to send a double value data from one application to another. I have created 2 applications, the one application will send latitude and longitude (both double value) to another application on every 10 mins.I have used service for it. And second application will just get these values and it will show to user. Can you just give me a little example for passing the double value
Thank you
I suggest you create a variable which contains all the information.
Then use
Intent i = new Intent(getApplicationContext(), NewActivity.class);
i.putExtra("new_variable_name","value");
startActivity(i);
Then in the new Activity, retrieve those values:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("new_variable_name");
}
i found the answer here
EDIT :
it also works with double doubleVal1 & 2 being your variables
Intent yourIntent = new Intent(thisActivity.this, nextActivity.class);
Bundle b = new Bundle();
b.putDouble("latitude", doubleVal1);
b.putDouble("longitude", doubleVal2);
yourIntent.putExtras(b);
startActivity(yourIntent);
Then, get it in your next Activity:
Bundle b = getIntent().getExtras();
double mLatitude = b.getDouble("latitude");
double mLongitude =b.getDouble("longitude);"`
it cames from this question pass double values in intent

Pass List<Double> value from one activity to other [duplicate]

This question already has answers here:
Pass ArrayList<Double> from one activity to another activity on android
(4 answers)
Closed 9 years ago.
I have a list like this,
List< Double> list_r = new ArrayList<Double>();
How can I pass this list from one activity to other ?
How can I pass this list from one activity to other ?
Then Make this list Static just like this:
public static List< Double> list_r = new ArrayList<Double>();
And Access this list in other activity like this:
private List<Double> list_my = ClassName.list_r;
Where ClassName is your Activity which consists (List< Double> list_r).
But make sure I am just showing a way of passing list. But by making List static It will consume memory even after you have finish the use of that arrayList.
You could use a double[] together with putExtra(String, double[]) and getDoubleArrayExtra(String).
Or you can use an ArrayList<Double> together with putExtra(String, Serializable) and getSerializableExtra(String) (the ArrayList part is important as it is Serializable, but the List interface is not).
You may use of bundle how to use: creating bundle and sending over to new activity or save arrayList in shared preferences: http://www.vogella.com/tutorials/AndroidFileBasedPersistence/article.html
intent.putExtra("list",list_r);
now on the other activity in onCreate:
getIntent().getParcelableArrayListExtra("list");
use intent.putExtra();
intent.putExtra("array_list", list_r );
Convert double to string, and put it in ArrayList.
In the Activity providing the data, use Intent#putExtra():
double double1 = 0.05;
double double2 = 0.02;
ArrayList <String []> list = new ArrayList <String[]>();
list.add (new String [] {String.valueOf(double_1),String.valueOf(double_2)});
Intent i = new Intent (this,YourTargetActivity.class);
i.putExtra("list", list);
startActivity(i);
Then to retrieve, use Intent#getSerializableExtra() and cast to ArrayList :
ArrayList <String[]> list = (ArrayList<String[]>) getIntent().getSerializableExtra("list");
double double1 = Double.parseDouble(list.get(0)[0]) ;
double double1 = Double.parseDouble(list.get(0)[1]) ;

Anyway to have more than one extra added to intent?

I want to have more than one extra added to Intent. One to hold a double and one to hold long. Is this possible?
If so, how would I do it and how would I get the information from each extra?
You can add as many extras to an Intent as your heart desires, they are all just key value data:
Intent intent = new Intent();
intent.putExtra("name", "MyName");
intent.putExtra("age", 35);
intent.putExtra("weight", 155.6);
And they can be retrieved using the same key names:
String name = intent.getStringExtra("name");
int age = intent.getIntExtra("age", 0);
double weight = intent.getDoubleExtra("weight", 0.0);
intent.putExtra(#ExtraDoubleKey, #ExtraDoubleValue);
intent.putExtra(#ExtraLongKey, #ExtraLongValue);
Where #ExtraDoubleKey is a string that you will use to access the extra (i.e. "price" or something), and #ExtraDoubleValue is the value of the extra (the double variable you wish to pass). Similarly for #ExtraLongKey and #ExtraLongValue.
Then to access the extras in your next activity you can use:
double doubleValue = getIntent().getExtras().getDouble(#ExtraDoubleKey);
long longValue = getIntent().getExtras().getLong(#ExtraLongKey);
to get the value of the double extra with the key #ExtraDoubleKey.
https://stackoverflow.com/a/11461530/776075
Devunwired is correct.
But the way i see is
you can keep only one value per Type.
Like one string, one int, one double etc..
You are not capable of containing 2 string values. or two integers.
I have experienced this on a program and i have overcome it by using
one string and one Boolean.
You can use Bundle and pass it as a parameter to the Intent.
Intent nextActivity = new Intent(this, Activity2.class);
Bundle passData = new Bundle(); //to hold your data
passDataBndl.putString("fname", fname); //put in some String. the first parameter to it is the id, and the second parameter is the value
nextActivity.putExtras(passDataBndl); //Add bundle to the Intent
startActivityForResult(nextActivity, 0); //Start Intent

Java Android passing data Array double

I have a question about intent in android application
i have an array in class1
double [][] tableCityPair = new double[100][100];
---here code to fill array---
in the end of class i want to send tableCityPair to another class, class2.
how i should declare for array in class2 ?
is this right?
Intent it = getIntent();
double tabelJarakAntarKota= it.getDoubleExtra("tableCityPair",3);
The Bundle class has methods for passing and retrieving an array of doubles:
void putDoubleArray(String, double[])
double [] getDoubleArray(String)
However, these work for one-dimensional arrays. You are attempting to pass a 2D array. I do not know of a direct way to do this. One way to achieve this would be to put N arrays of doubles in a loop.
for(int i=0;i<tableCityPair.length;i++){
bundle.put("tableCity"+i, tableCityPair[i]);
}
And at the receiving end, you do:
double [] aPair = it.getExtras().getDoubleArray("tableCity"+i);
I'm not sure about the performance implications of this though; since you would be adding 100 extras as part of the bundle.
There could be a better way (perhaps make your pair a List<List<Double>> and then implement Parcelable) but I haven't tried any of it so I wouldn't suggest it.
double [][] tableCityPair = new double[100][100];
and then...
make intent it=....
then
put: `it.putExtra("size",tableCityPair.length)`
then:
for(i=0;i<tableCitypair.length;i++)
{
it.putExtra("table"+i,tableVityPair[i][]);
}
THEN in CLASS B
double [][] tableCityPairinB = new double[100][100];
for(i=0;i<getIntent().getExtras("size");i++)
{
double[i][]=getIntent().getExtras("table"+i);
}

Sending LinkedHashMap to intent

I want send LinkedHashMap to another Intent. But I don't known what method for extras is allowable.
Bundle extras = getIntent().getExtras();
LinkedHashMap<Integer, String[]> listItems = extras.get(LIST_TXT);
You cannot reliably send a LinkedHashMap as an Intent extra. When you call putExtra() with a LinkedHashMap, Android sees that the object implements the Map interface, so it serializes the name/value pairs into the extras Bundle in the Intent. When you want to extract it on the other side, what you get is a HashMap, not a LinkedHashMap. Unfortunately, this HashMap that you get has lost the ordering that was the reason you wanted to use a LinkedHashMap in the first place.
The only reliable way to do this is to convert the LinkedHashMap to an ordered array, put the array into the Intent, extract the array from the Intent on the receiving end, and then recreate the LinkedHashMap.
See my answer to this question for more gory details.
The best way is to convert it to ArrayList of the same type examble :
LinkedHashSet<Long> uniqueIds = new LinkedHashSet();
ArrayList<Long> ids = new ArrayList(uniqueIds);
Intent intent = new Intent(getContext(), UserStoryActivity.class);
intent.putParcelableArrayListExtra(FLAG_USERS_STORY_LIST_IDS, (ArrayList) ids);
startActivity(intent);
Thanks #David Wasser for pointing out this issue.
I have another approach is to use Gson to parse your whatever map to json string, then put into the intent. Less drama!
Code send:
gson.toJson(data)
Code get back:
Kotlin:
gson.fromJson<LinkedHashMap<Int, Int>>(
it.getStringExtra(ARG_DATA),
object : TypeToken<LinkedHashMap<Int, Int>>() {}.type
)
Java:
gson.fromJson(
it.getStringExtra(ARG_DATA),
new TypeToken<ArrayList<Mountain>>(){}.getType()
);

Categories

Resources