Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass ArrayList using putStringArrayListExtra()

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); 
like image 262
Miya Avatar asked Oct 27 '10 05:10

Miya


People also ask

Can we pass ArrayList as parameter in Java?

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.

How pass ArrayList from activity to fragment in Android?

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.

How to pass generic ArrayList in putstringarraylistextra in Java?

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?

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.

How to pass an ArrayList as an argument to a function?

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); }

What is the use of ArrayList class in Java?

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.


1 Answers

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");   
like image 190
user2227186 Avatar answered Oct 18 '22 13:10

user2227186