In my app, i need to send a Two dimensional Array and two more integer values form on Activity to Another with the help of Intent method.
This is done as ..
Intent i = new Intent(getApplicationContext(), ViewActivity.class);
Bundle postbundle = new Bundle();
String[][] X={{"abc"},{"def"}};
postbundle.putSerializable("data", X);
i.putExtra("A", postbundle);
i.putExtra("albumid", position);
i.putExtra("Bigcard",bigcard);
here am using .putSerializable method to place an array into bundle.
So to access these data in the receiver Activity am using
Bundle bundle = getIntent().getBundleExtra("A");
String[][] ABC=(String[][]) bundle.getSerializable("data");
Log.e("Array is",""+ABC);
but I got java.lang.NullPointerException error message..
Whith out use of " Static " declaration how can i get these values from bundle here (in the receiver Activity..)
Let me out pls from this ..
step-1:Write a seperate bean class and save into another file
public class MyBean implements Serializable{
String[][] data = null;
public void set2DArray(String[][] data){
this.data = data;
}
public String[][] get2DArray(){
return data;
}
}
step-2:In the calling activity
Intent intent = new Intent(this, Second.class);
String data[][] = new String[][] {{"1","kumar"},{"2","sona"},{"3","kora"},{"1","pavan"},{"2","kumar"},{"3","kora333"}};
MyBean bean = new MyBean();
bean.set2DArray(data);
Bundle b = new Bundle();
b.putSerializable("mybean", bean);
intent.putExtra("obj", b);
startActivity(intent);
step-3:In the caller activity
Bundle b = getIntent().getBundleExtra("obj");
MyBean dData = (MyBean) b.getSerializable("mybean");
String[][] str =dData.get2DArray();
Not a real answer, but a try:
what happens if You try:
Intent intent = getIntent();
int a = intent.getIntExtra("albumid"); //if Your value is an int, otherwise use String
//getStringExtra or whatever Your value is
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'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 have two string Arrays, contactNames in the main Activity class, and address [] in the second activity class. when i click on the one of the names, it should show me the address details for that name. How should I create an intent for two string arrays in Android.
class ContactList
Intent i = new Intent (MainActivity.this, Details.class);
Bundle bundle = new Bundle ();
bundle.putStringArray("key", address);
i.putExtras(bundle);
startActivity(i);
}
class Details
Bundle b = this.getIntent().getExtras();
String array [] = b.getStringArray("key");
tvEmail.setText(array.toString());
}
thanks.
How should I create an intent for two string arrays in Android.
You can add another array (or any other data type) but you need to use a different key name. Example...
String[] array1;
String[] array2;
// Populate arrays here
Intent i = new Intent (MainActivity.this, Details.class);
Bundle bundle = new Bundle ();
bundle.putStringArray("key1", array1);
bundle.putStringArray("key2", array2);
i.putExtras(bundle);
startActivity(i);
Also note that you don't need to use a Bundle directly. You can use the Intent method putExtra(String name, String[] value). For example...
String[] array1;
String[] array2;
// Populate arrays here
Intent i = new Intent (MainActivity.this, Details.class);
i.putExtra("key1", array1);
i.putExtra("key2", array2);
startActivity(i);
How to pass a 2d String array to new activity???
and then in new activity, How can i retrieve the array??
You have to use:
String [][]str;
Intent summaryIntent = new Intent(this, Second.class);
Bundle b=new Bundle();
b.putSerializable("Array", str);
summaryIntent.putExtras(b);
startActivity(summaryIntent);
For Recieving the Array use:
Bundle b = getIntent().getExtras();
String[][] list_array = (String[][])b.getSerializable("Array");
Thanks
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 !