数组通过意图的对象列表

adi*_*dit 16 java android

我知道你可以通过意图传入String的数组列表,但是如果它是我定义的某个对象的数组列表呢?说一个自行车列表,我该怎么做?

Ted*_*opp 23

您可以使对象实现Parcelable并使用putParcelableArrayListExtra.或者,您可以以某种方式序列化对象并放置序列化对象的字节数组.

  • 请举例而不是建议 (2认同)

wan*_*nik 20

这是一个例子.MainActivity将人员列表发送到OtherActivityvia 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);
Run Code Online (Sandbox Code Playgroud)

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

    }
}
Run Code Online (Sandbox Code Playgroud)

  • `intent.putExtra("list",list);`给我一个错误:`无法解析方法` (6认同)
  • 请注意,使用*Serializable*会导致性能下降,因此您实际上应该使用*Parcelable*. (5认同)

Tig*_*ian 9

还有一种方法 - 你可以将对象列表序列化为某种字符串表示形式(让它成为JSON),然后将字符串值检索回列表

// 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)); 
Run Code Online (Sandbox Code Playgroud)


小智 7

一个更好的主意是为要放入其Intent的数组列表的对象实现Parcelable接口。例如:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后您可以说应用程序代码:

bundle.putParcelableArrayList("personList", personList);
Run Code Online (Sandbox Code Playgroud)