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); 
                If we want to pass an ArrayList as an argument to a function then we can easily do it using the syntax mentioned below. In the code above, we created an ArrayList object named 'list' and then we passed it to a function named modifyList.
If you want to pass an ArrayList to your fragment, then you need to make sure the Model class is implements Parcelable. Here i can show an example. then you can add ArrayList<ObjectName> to a Bundle object. ArrayList<ObjectName> arraylist = new Arraylist<ObjectName>(); Bundle bundle = new Bundle(); bundle.
You have to define ArrayList of type String. you can't pass Generic ArrayList in putStringArrayListExtra. Below is the correct code. ----- ArrayList<String> al = new ArrayList<String> (); ------ ------- list_bundle.putStringArrayListExtra ("lists",al); ------
How to pass an arrayList to another activity using intents in Android? This example demonstrates how do I pass an arrayList to another activity using intends in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
If we want to pass an ArrayList as an argument to a function then we can easily do it using the syntax mentioned below. Example: ArrayList<Integer> list = new ArrayList<>(); modifyList(list); public static void modifyList(ArrayList<Integer> list){ list.add(69); list.add(98); }
ArrayList class in Java is basically a resize-able array i.e. it can grow and shrink in size dynamically according to the values that we add or remove to/from it. It is present in java.util package.
Try this It worked for me
1st Activity
ArrayList<String> ar=new ArrayList<String>();
ar.add("Apple");
ar.add("Banana");
Intent i=new Intent(this,Route.class);
i.putStringArrayListExtra("list", ar);
startActivity(i);
2nd Activity
ArrayList<String> ar1=getIntent().getExtras().getStringArrayList("list");   
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With