1

I'm trying to get an Arraylist to work in another Activity on Android. All is well. The arrray list is created, however, I cannot get Intent to work.

What way is there to get an arraylist to another variable?

Here is the code I'm using to generate the arrayList:

public List<Contact> getAllContacts() {
List<Contact> ContactpList = new ArrayList<Contact>();

How will I go about making this arraylist available in my other activities?

InnocentKiller
  • 5,234
  • 7
  • 36
  • 84
Dragon.Void
  • 83
  • 1
  • 11

3 Answers3

3

This is your activity were you have array list Right:

public List<Contact> getAllContacts() {
List<Contact> ContactpList = new ArrayList<Contact>();

now when u call another activity then send the array object like this:

Intent intent = new Intent(youractivity.this,youractivity.class);
intent.putExtra("ContactpList ", ContactpList );
startActivity(intent);

now get this array in the activity which you have called and were u want to get the array:

ArrayList<String> resultArray = getIntent().getStringArrayListExtra("ContactpList ");

Hope it is work for you.

Farhan Shah
  • 2,344
  • 7
  • 28
  • 54
0

try this :

Intent i = getIntent();  
list = i.getStringArrayListExtra("list");

or other way that to make static your arraylist and access via class name.

Harshit Rathi
  • 1,862
  • 2
  • 18
  • 25
0

You can create an ArrayList<Contact> list and pass it to another activity via bundle like this:

passing: <your intent>.putExtra("List", list);

getting: getIntent().getSerializableExtra("List");

make sure Contact class is Serializable : add public Class Contact implements Serializable {}

OR you can make a Static variable :

public static ArrayList<Contact> list = new ArrayList<Contact>();
Jilberta
  • 2,836
  • 5
  • 30
  • 44