I'm trying to pass a Double and a long array from one activity to another. I'm also passing a string. The string is successfully passed but the arrays are not - its probably something simple but I just can't see it.
I move from one activity to another on the click of a button with the following code:
Intent intent = new Intent(stockDownload.this, StockDisplay.class);
//need to find a way to access information referring to specific button - stock return and name
ArrayList<Double> returnsforintent = (ArrayList<Double>) stockprices.get(view.getId());
ArrayList<Long> errortrial = datesintent;
Double[] returnstopass= returnsforintent.toArray(new Double[returnsforintent.size()]);
Long[] datestopass = datesintent.toArray(new Long[datesintent.size()]);
intent.putExtra(GRAPHRETURNS, returnstopass)
;
intent.putExtra(GRAPHSTOCKS, stocks.get(view.getId()));
intent.putExtra(GRAPHDATES, datestopass);
startActivity(intent);
I use the following to receive the arrays and string:
Bundle intent = getIntent().getExtras();
double[] stockprice = intent.getDoubleArray(stockDownload.GRAPHRETURNS);
long[] dates = intent.getLongArray(stockDownload.GRAPHDATES);
String stockname = intent.getString(stockDownload.GRAPHSTOCKS);
You can Use.
1. void putDoubleArray(String, double[]) //for passing array
2. double [] getDoubleArray(String) //for retriving array
in Bundle class.
Here in your case I think double[](Primitive type double) and Double[](Object array) is creating problem.(Same thing for Long[] and long[])
As method need parameter passed as double[] not Double[]
Related
I am sending one 2D array from one activity to another using bundle, but on the reception end I get the error
Caused by: java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.lang.String[][]
the code for your consideration is provided
obj_res = (EditText) findViewById(R.id.et_result);
Bundle bundle = getIntent().getExtras();
String[][] table = (String[][])bundle.getSerializable("table");
for(int a=0;a<30;a++) { //to display array data
for (int b = 0; b < 4; b++) {
obj_res.append(String.valueOf(table[a][b]));
}
obj_res.append("\n");
}
please help me in this mater of if there is any other convenient way to pass 2D array
While sending:
Bundle b=new Bundle();
b.putSerializable("arr", your2dArray);
Intent i = new Intent(this, OtherActivity.class);
i.putExtras(b);
startActivity(i);
While receiving:
Bundle b=this.getIntent().getExtras();
Object[] objectArray = (Object[]) b.getSerializable("arr");
String[][] arr = new String[objectArray.length][];
for(int i=0;i<objectArray.length;i++) {
arr[i]=(String[]) objectArray[i];
}
The simple simple way to send data serial objects across activities is you should use GSON library by google
First covert the object to json text then send text to second activity and in the second activity convert that json back to object
Hope this might help.
I have a function in my Android code where I get a bundle as input:
void f(Bundle data)
This data is actually in the form of a json. Suppose it is in the following format:
{"a":"x", "b":"y", "content":{"a1":"x1", "b1":"y1"}}
In such cases, if I want to get the value of a, or b, then I would need to do String a = data.getString("a"); which would fetch me the string "x". Similarly, String content = data.getString("content") would return me the string {"a1":"x1", "b1":"y1"}}. But I cannot figure out how to get the specific values inside content itself. Is there any way by which I can get content as another bundle just like data so that I can get the values inside it by doing content.getString("a1") or something like that. Is that possible?
JSONObject jOBj = new JSONobject(data.getString("content"));
String a1 = jOBj.getString("x1");
String b1 = jOBj.getString("y1");
try this
To get value from bundle and create JSon object/ Hashmap
private void createFlatJSon(Bundle appRestrictions,JSONObject jsonObject) throws JSONException{
for (String key : appRestrictions.keySet()) {
if (appRestrictions.get(key) instanceof Bundle) {
createFlatJSon((Bundle) appRestrictions.get(key),jsonObject);
}else if (appRestrictions.get(key) instanceof Parcelable[]){
for (int i=0;i< ((Parcelable[]) appRestrictions.get(key)).length; i++){
createFlatJSon((Bundle)((Parcelable[]) appRestrictions.get(key))[i],jsonObject);
}
//Log.e("KEY skipped",appRestrictions.get(key).toString());
}else{
Log.e("KEY: ",key+" Value:"+appRestrictions.get(key).toString());
jsonObject.put(key,appRestrictions.get(key).toString());
}
}
}
I'm attempting to pass two integers from my Main Page activity (a latitude and longitude) to a second activity that contains an instance of Google Maps that will place a marker at the lat and long provided. My conundrum is that when I retrieve the bundle in the Map_Page activity the integers I passed are always 0, which is the default when they are Null. Does anyone see anything glaringly wrong?
I have the following stored in a button click OnClick method.
Bundle dataBundle = new Bundle();
dataBundle.putInt("LatValue", 39485000);
dataBundle.putInt("LongValue", -80142777);
Intent myIntent = new Intent();
myIntent.setClassName("com.name.tlc", "com.name.tlc.map_page");
myIntent.putExtras(dataBundle);
startActivity(myIntent);
Then in my map_page activity I have the following in onCreate to pick up the data.
Bundle extras = getIntent().getExtras();
System.out.println("Get Intent done");
if(extras !=null)
{
System.out.println("Let's get the values");
int latValue = extras.getInt("latValue");
int longValue = extras.getInt("longValue");
System.out.println("latValue = " + latValue + " longValue = " + longValue);
}
Geeklat,
You don't need to use Bundle in this case.
Do your puts like this...
Intent myIntent = new Intent();
myIntent.setClassName("com.name.tlc", "com.name.tlc.map_page");
myIntent.putExtra("LatValue", (int)39485000);
myIntent.putExtra("LongValue", (int)-80142777);
startActivity(myIntent);
Then you can retrieve them with...
Bundle extras = getIntent().getExtras();
int latValue = extras.getInt("LatValue");
int longValue = extras.getInt("LongValue");
System.out.println("Let's get the values");
int latValue = extras.getInt("latValue");
int longValue = extras.getInt("longValue");
Not the same as
myIntent.putExtra("LatValue", (int)39485000);
myIntent.putExtra("LongValue", (int)-80142777);
Also it might be because you do not keep the name of the Int exactly the same throughout your code. Java and the Android SDK are Case-sensitive
I want to pass a 2-D String Array to another activity.
My S2-D String Array (String[][]) is 'selected_list'.
code is :
Bundle list_bundle=new Bundle();
list_bundle.putStringArray("lists",selected_list);
Intent list_Intent = new Intent(v.getContext(), view_selected_items.class);
startActivityForResult(list_Intent, 2);
But putStringArray("lists",selected_list) is showing a error like we can pass only 1-Dimensional Array (String[]).
Thanks
You can use putSerializable. Arrays are serializable, provided their elements are.
To store:
list_bundle.putSerializable("lists", selected_list);
To access:
String[][] selected_list = (String[][]) bundle.getSerializable("lists");
This finally works well for me : Thanks to Matthew and Mehdiway
To start a new activity (sending String[][] and String):
String[][] arrayToSend=new String[3][30];
String stringToSend="Hello";
Intent i = new Intent(this, NewActivity.class);
i.putExtra("key_string",stringToSend);
Bundle mBundle = new Bundle();
mBundle.putSerializable("key_array_array", arrayToSend);
i.putExtras(mBundle);
startActivity(i);
To access in NewActivity.onCreate:
String sReceived=getIntent().getExtras().getString("key_string");
String[][] arrayReceived=null;
Object[] objectArray = (Object[]) getIntent().getExtras().getSerializable("key_array_array");
if(objectArray!=null){
arrayReceived = new String[objectArray.length][];
for(int i=0;i<objectArray.length;i++){
arrayReceived[i]=(String[]) objectArray[i];
}
}
As Matthew said, you can put the string array in the bundle like this
Bundle list_bundle=new Bundle();
list_bundle.putStringArray("lists",selected_list);
This works very well, but when you try to get the array via (String[][]) bundle.getSerializable("lists"); it gives a
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to java.util.String
So what you have to do is this :
Object[] objectArray = (Object[]) getIntent().getExtras().getSerializable("lists");
int horizontalLength = 0;
if (objectArray.length > 0)
horizontalLength = ((Object[]) objectArray[0]).length; // See explanation to understand the cast
String[][] my_list = new String[objectArray.length][horizontalLength];
int i = 0;
for(Object row : objectArray)
{
my_list[i][0] = ((String[])row)[0];
my_list[i++][1] = ((String[])row)[1];
}
Explanation :
When you get the array as I Serializable object it doesn't behave the same way a simple String array does, instead it considers every row as a separate array, so what you have to do is to read the Serializable object as a one dimensional array, and then cast every row of it to a String[] array. Et voila !
I have a service that produces as result a SortedSet object. I need to pass it to another Intent that will use it. First is put in the notification area, then when the user actions it it will need to fire the activity.
As it it's own SortedSet is not Parcelable nor Serializable so it won't work. One single item in SortedSet is already parcelable and it's used fine, but I need the whole set.
How to pass a SortedSet as an Intent's extra?
I found out:
// passes as serializable and as Array
intent.putExtra("results", result.toArray());
Then read out by reconstructing the array into the SortedSet.
In my case it was:
Serializable s = this.getIntent().getSerializableExtra("results");
Object[] o = (Object[]) s;
if (o != null) {
resultSet = new TreeSet<RatedMessage>(new Comp());
for (int i = 0; i < o.length; i++) {
if (o[i] instanceof RatedMessage) {
resultSet.add((RatedMessage) o[i]);
}
}
}
Now I can use resultSet further.