Hi
I want to pass an Arraylist from one activity to another.
I use putStringArrayListExtra(), but there shows an error :
"The method putStringArrayListExtra(String,ArrayList is undefined for the type bundle."
Is there any other method available for passing ArrayList?
String test[]=new String[3];
ArrayList<String[]> al=new ArrayList<String[]>();
int x,y;
test[0]="1";
test[1]="2";
test[2]="3";
al.add(test);
test = new String[3];
test[0]="4";
test[1]="5";
test[2]="6";
al.add(test);
Bundle list_bundle=new Bundle();
list_bundle.putStringArrayListExtra("lists",al);
Intent list_intent= new Intent(v.getContext(), view_all_selected.class);
list_intent.putExtras(list_bundle);
startActivityForResult(list_intent, 2);
Please help me..
putStringArrayListExtra is a method of Intent class. In the code above try to call:
list_intent.putStringArrayListExtra("lists",al);
and remove these lines:
Bundle list_bundle=new Bundle();
list_bundle.putStringArrayListExtra("lists",al);
list_intent.putExtras(list_bundle);
Related
This question already has answers here:
Sending arrays with Intent.putExtra
(3 answers)
Closed 5 years ago.
how to send the array in intent in android studio?
i have two array list
Eve_id[];//here i had stored 5 diffrent values.
sc_id[];//here i had stored 5 diffrent values.
now i have to send these two array to next activity,i have used the code like this
1st activity`
Intent k = new Intent(context, Receiver.class);
k.putExtra("Event_id", Eve_id);
k.putExtra("schedule_id", sc_id);`
2nd activity
Bundle extras = getIntent().getExtras();
long[] event_id = extras.getLongArray("Event_id");
int[] schedule_id = extras.getIntArray("schedule_ids");
i dont know weather it is correct are not ,but i am not able to receive any data!
any one can suggest how to solve this!
You can use Bundle for That
Intent passArray = new Intent(MainActivity.this ,YourSecondClass.class);
Bundle bundle = new Bundle();
bundle.putStringArray("my_array", array);
startActivity(passArray , bundle);
passArray.putExtras(bundle);
startActivity(passArray);
Use ArrayList instead of array:
ArrayList<Long> eventIds = new ArrayList<>();
ArrayList<Integer> scheduleIds = new ArrayList<>();
fill them and put into bundle:
Bundle extras = new Bundle();
extras.putIntegerArrayList("schedule_ids",scheduleIds);
extrass.putLongArrayList("Event_id", eventIds);
Intent k = new Intent(context, Receiver.class);
k.putExtras(extras);
and then extract this values in Receiver activity:
Bundle extras = getIntent().getExtras();
ArrayList<Long> eventIds = extras.getLongArrayList("Event_id");
ArrayList<Integer> scheduleIds = extras.getIntegerArrayList("schedule_ids");
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);
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
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);
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 !