Dragon.Void
Dragon.Void

Reputation: 83

Java / Android ArrayList in different Activity

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?

Upvotes: 1

Views: 643

Answers (3)

Farhan Shah
Farhan Shah

Reputation: 2342

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.

Upvotes: 3

Jilberta
Jilberta

Reputation: 2866

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

Upvotes: 0

Harshit Rathi
Harshit Rathi

Reputation: 1862

try this :

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

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

Upvotes: 0

Related Questions