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?

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

try this :

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

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.