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");
Related
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 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'm looking for a way to pass, via bundle, an
ArrayList<Integer[]>
object to a Fragment (after activity is already created, so i can't use intent). By looking to android api, no method seems to do what i'm looking for. How can i do?
Sending activity:
final Intent intent = new Intent(this, SecondActivity.class);
Bundle extraBundle = new Bundle();
extraBundle.putIntegerArrayList("arraylist", [put array list here]);
intent.putExtras(extraBundle);
intent.setComponent(new ComponentName("com.myapp", "com.myapp.SecondActivity"));
startActivity(intent);
Receiving activity's onCreate():
final Intent intent = getIntent();
final Bundle extraBundle = intent.getExtras();
ArrayList<Integer> myIntegerArrayList = extraBundle.getIntegerArrayList("arraylist");
You can change "arraylist" to what you want in the setter and getter method calls, they just need to be the same.
I used Gson, convert to JSONArray and send via Bundle. But it may affects performance.
In First Activity
Intent activity = new Intent(MyActivity.this,NextActivity.class);
activity.putExtra("myArrayList", new Gson().toJson(myArrayList);
startActivity(activity);
In other activity..
Sting myArrayList;
Bundle extras = getIntent().getExtras();
if (extras != null) {
myArrayList= extras.getString("myArrayList");
Type listOfInteger = new TypeToken<List<Integer>>(){}.getType();
String s = new Gson().toJson(myArrayList, listOfInteger);
List<Integer> myList = new Gson().fromJson(s, listOfInteger);
}
int[] intarray = new int[] {4,5,6,7,8};
Bundle bundle = new Bundle();
bundle.putIntArray("integerarray", intarray);
Or an ArrayList with int arrays?
For that i think you should use Parcable.
Or you could try to send the individual intArrays via the bundle and put them together in the receiving Activity.
Like this:
int[] intarray1 = new int[] {4,5,6,7,8};
int[] intarray2 = new int[] {4,5,6,7,8};
int[] intarray3 = new int[] {4,5,6,7,8};
Bundle bundle = new Bundle();
bundle.putIntArray("INT_ARRAY1", intarray1);
bundle.putIntArray("INT_ARRAY2", intarray2);
bundle.putIntArray("INT_ARRAY3", intarray3);
Intent intent = new Intent(this,NewActivity.class)
intent.putExtras(bundle);
startActivity(intent)
Then in NewActivity.java you should create an array and get the bundle and fill the array with the received arrays.
you can pass the Array List of integers by using the method putIntegerArrayList
Here is the code snippet
ArrayList<Integer> values=new ArrayList<>();
values.add(1);
values.add(60);
values.add(75);
values.add(120);
Bundle extras = new Bundle();
extras.putIntegerArrayList(EXTRA_KEY,values);
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 !