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.
Related
I need to pass a 2D array as an intent onto another activity.
This code works for sending the 2D array, the problem is retrieving it in the next activity.
i++;
Intent tryAgain = getIntent();
tryAgain.putExtra("index", i);
tryAgain.putExtra("from", "next");
int [][] previousValues = getSelectedIndices();
Toast.makeText(AnotherActivity.this, "Bang " + previousValues[3][3], Toast.LENGTH_SHORT).show();
Bundle mBundle = new Bundle();
mBundle.putSerializable("array", previousValues);
tryAgain.putExtras(mBundle);
AnotherActivity.this.finish();
startActivity(tryAgain);
I have already tried this code below to retrieve the 2D array, it does NOT work.
else if(getIntent().getStringExtra("from").equals("next")){
i=index;
int [][] arrayReceived=null;
Object[] objectArray = (Object[]) getIntent().getExtras().getSerializable("array");
if(objectArray!=null){
arrayReceived = new int[objectArray.length][];
for(int i=0;i<objectArray.length;i++){
arrayReceived[i]=(int[]) objectArray[i];
}
}
You can do something for receiving array like this as an example:
Bundle b = getIntent().getExtras();
int[][] list_array = (int[][])b.getSerializable("array");
I have a list of json array response with multiple userinformation. Created a bundle and successfully passed it to next activity. Created another bundle with user selected date and time.But no luck to second bundle to the same activity because i can be able to pass only one bundle to same activity.
My actual problem is how to pass second bundle in putExtras to same activity
Json Response
{
"userinfo": [
{
"address": "Tambaram",
"name": "Vishranthi"
},
{
"address": "Medavakkam",
"name": "Sophia"
},
]
}
Bundle creation code:
JSONArray infoarray = obj.getJSONArray("Info");
Bundle h = new Bundle();
for (int i = 0; i < infoarray.length(); i++) {
Bundle b = new Bundle();
JSONObject infoobject = infoarray.getJSONObject(i);
String name = infoobject.getString("name");
String address = infoobject.getString("address");
b.putString("name", name);
b.putString("address", address);
h.putBundle(Integer.toString(i), b);
System.out.println(b);
}
Intent i = new Intent(context, Secondpage.class);
Bundle d=new Bundle();
d.putString("date", text1);
d.putString("time", text2);
i.putExtras(h);
System.out.println(h);
context.startActivity(i);
if you want to put two bundles into your intent you have to use
Intent putExtra (String name,
Bundle value)
something like this:
Intent i = new Intent(context, Secondpage.class);
i.putExtra("bundleH", h);
i.putExtra("bundleD", d);
ref
instead of create another Bundle to the date and time use the same one
You cannot, so the easiest solution would be to pass the json youre parsing into the second activity and the values you want on the second bundle as primitives, then on the second activity parse json into primitive data and retrieve date & time.
You don't need to deserialize a JSONObject or JSONArray, just put it in Bundle as String.
Bundle bundle = new Bundle();
bundle.putString("MyBundle", infoarray.toString());
So at next Activity, get the JSONArray
JSONArray newJArray = new JSONArray(bundle.getString("MyBundle",""));
A little tip: Avoid declare variables inside loops to avoid memory leaks.
I think you should change the approach a bit:
do this in your first activity your you have the json array
Intent i = new Intent(context, Secondpage.class);
i.putExtra("userinfo", infoarray.toString());
and in your second activity do the rest code
if (getIntent().getExtras() != null && getIntent().getExtras().containsKey("userinfo")) {
try {
JSONArray infoarray = new JSONArray(getIntent().getExtras().getString("userinfo"));
for (int i = 0; i < infoarray.length(); i++) {
]
JSONObject infoobject = infoarray.getJSONObject(i);
String name = infoobject.getString("name");
String address = infoobject.getString("address");
//your code parse user list
}
} catch (JSONException e)
}
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[]
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.