简体   繁体   中英

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?

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.

try this :

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM