简体   繁体   中英

Array List of objects via intent

I know you can pass in an array list of String via intent, but what if it's an array list of some object that I defined? Say an array list of Bicyles, how do I do this?

You can make your objects implement Parcelable and use putParcelableArrayListExtra . Alternatively, you can serialize your objects in some way and put the byte array of your serialized objects.

This is an example. MainActivity sends list of persons to OtherActivity via Intent .

class Person implements Serializable {
    int id;
    String name;

    Person(int i, String s) {
        id = i;
        name = s;
    }
}

public class TestAndroidActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ArrayList<Person> list = new ArrayList<Person>();
        list.add(new Person(1, "Tom"));
        list.add(new Person(5, "John"));

        Intent intent = new Intent(this, OtherActitity.class);
        intent.putExtra("list", list);
        startActivity(intent);

OtherActivity.java

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class OtherActitity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.other);

        Intent i = getIntent();
        ArrayList<Person> list = (ArrayList<Person>) i
                .getSerializableExtra("list");
        Toast.makeText(this, list.get(1).name, Toast.LENGTH_LONG).show();

    }
}

One more way - you can serialize list of objects into some kind of string representation (let it be JSON) and then retrieve the string value back to list

// here we use GSON to serialize mMyObjectList and pass it throught intent to second Activity
String listSerializedToJson = new Gson().toJson(mMyObjectList);
intent.putExtra("LIST_OF_OBJECTS", listSerializedToJson);
startActivity(intent);

// in second Activity we get intent and retrieve the string value (listSerializedToJson) back to list 
String listSerializedToJson = getIntent().getExtras().getString("LIST_OF_OBJECTS");
mMyObjectList = new Gson().fromJson(objectsInJson, MyObject[].class); // in this example we have array but you can easy convert it to list - new ArrayList<MyObject>(Arrays.asList(mMyObjectList)); 

A better idea is to implement Parcelable interface for the object whose arraylist you want to put in the Intent .For Example :

public class Person implements Parcelable{

    private int id;
    private String name;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public int describeContents() {
        return this.hashCode();
    }
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(id);
        dest.writeString(name);
    }
}

And then application code you can say :

bundle.putParcelableArrayList("personList", personList);

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