Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass arraylist from one activity to other

Tags:

android

How can I pass ArrayList from one activity to other activity?

like image 620
think shra1 Avatar asked Jan 24 '11 10:01

think shra1


People also ask

How do I move a list from one activity to another?

We can send the data using the putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.

How pass ArrayList from one 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.


2 Answers

It depends on the type of arraylist

  • putIntegerArrayListExtra(String name, ArrayList<Integer> value)

  • putParcelableArrayListExtra(String name, ArrayList<? extends Parcelable> value)

  • putStringArrayListExtra(String name, ArrayList<String> value)

  • putCharSequenceArrayListExtra(String name, ArrayList<CharSequence> value)

Then you can read from you next activity by replacing put with get with key string as argument,eg

myIntent.getStringArrayListExtra("arrayPeople");
like image 91
Labeeb Panampullan Avatar answered Oct 21 '22 10:10

Labeeb Panampullan


You can create one bundle in bundle put parceable array list provided by labeeb and set to intent here is the code for

Intent i = new Intent(this,name.class);
Bundle b = new Bundle();
b.putIntegerArrayListExtra(String name, ArrayList<Integer> value);
//b.putParcelableArrayListExtra(String name, ArrayList<? extends Parcelable> value);
//b.putStringArrayListExtra(String name, ArrayList<String> value);
i.putExtra(String name,b);
startActivity(i);

And get data into another activity like

//pseudo code
Bundle b = getIntent().getExtra().putParcelableArrayListExtra(String name);
like image 34
Anand Avatar answered Oct 21 '22 11:10

Anand