You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从另一个Activity中获取存储自定义对象的ArrayList?

Hey there! Let's figure out how to get that ArrayList of custom objects from another Activity and read its data in your current one. Since your objects hold three strings, here are the most practical approaches you can use:

方法1:让自定义对象实现Parcelable(Android推荐方案)

Parcelable is Android's built-in interface for efficient object serialization, and it's the go-to choice for passing data between components. Here's how to set it up:

First, update your custom class to implement Parcelable. Let's say your class is named MyCustomObject:

public class MyCustomObject implements Parcelable {
    private String str1;
    private String str2;
    private String str3;

    // 常规构造函数
    public MyCustomObject(String str1, String str2, String str3) {
        this.str1 = str1;
        this.str2 = str2;
        this.str3 = str3;
    }

    // 从Parcel中恢复对象的构造函数
    protected MyCustomObject(Parcel in) {
        str1 = in.readString();
        str2 = in.readString();
        str3 = in.readString();
    }

    // 必须实现的Creator静态类
    public static final Creator<MyCustomObject> CREATOR = new Creator<MyCustomObject>() {
        @Override
        public MyCustomObject createFromParcel(Parcel in) {
            return new MyCustomObject(in);
        }

        @Override
        public MyCustomObject[] newArray(int size) {
            return new MyCustomObject[size];
        }
    };

    // describeContents方法:通常返回0即可,除非你的对象包含特殊文件描述符
    @Override
    public int describeContents() {
        return 0;
    }

    // 将对象数据写入Parcel的方法
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(str1);
        dest.writeString(str2);
        dest.writeString(str3);
    }

    // 添加getter方法,方便后续读取字段值
    public String getStr1() { return str1; }
    public String getStr2() { return str2; }
    public String getStr3() { return str3; }
}

在源Activity(创建ArrayList的页面)传递数据

Once you've populated your ArrayList, attach it to an Intent when starting the target Activity:

// 创建并填充ArrayList
ArrayList<MyCustomObject> customList = new ArrayList<>();
customList.add(new MyCustomObject("First string", "Second string", "Third string"));
customList.add(new MyCustomObject("Another first", "Another second", "Another third"));

// 启动目标Activity并传递数据
Intent intent = new Intent(SourceActivity.this, TargetActivity.class);
intent.putParcelableArrayListExtra("CUSTOM_OBJECT_LIST_KEY", customList);
startActivity(intent);

在目标Activity(当前页面)读取数据

Retrieve the ArrayList from the Intent in your target Activity's onCreate method:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_target);

    // 获取传递过来的ArrayList
    ArrayList<MyCustomObject> receivedList = getIntent().getParcelableArrayListExtra("CUSTOM_OBJECT_LIST_KEY");

    // 检查数据不为空后,遍历读取内容
    if (receivedList != null) {
        for (MyCustomObject obj : receivedList) {
            String firstStr = obj.getStr1();
            String secondStr = obj.getStr2();
            String thirdStr = obj.getStr3();
            // 这里可以做你需要的操作,比如显示到TextView或者处理逻辑
        }
    }
}
方法2:让自定义对象实现Serializable(简单但效率较低)

If you want a quicker setup with less boilerplate code, you can use Java's Serializable interface. Note that this is less efficient than Parcelable, so it's better suited for small datasets.

更新自定义类

public class MyCustomObject implements Serializable {
    private String str1;
    private String str2;
    private String str3;

    public MyCustomObject(String str1, String str2, String str3) {
        this.str1 = str1;
        this.str2 = str2;
        this.str3 = str3;
    }

    // 同样需要getter方法
    public String getStr1() { return str1; }
    public String getStr2() { return str2; }
    public String getStr3() { return str3; }
}

源Activity传递数据

ArrayList<MyCustomObject> customList = new ArrayList<>();
// 填充数据...

Intent intent = new Intent(SourceActivity.this, TargetActivity.class);
intent.putExtra("CUSTOM_OBJECT_LIST_KEY", customList);
startActivity(intent);

目标Activity读取数据

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_target);

    // 注意需要强制类型转换
    ArrayList<MyCustomObject> receivedList = (ArrayList<MyCustomObject>) getIntent().getSerializableExtra("CUSTOM_OBJECT_LIST_KEY");

    if (receivedList != null) {
        // 遍历处理数据
        for (MyCustomObject obj : receivedList) {
            // 操作字段值
        }
    }
}
额外注意事项
  • 优先选Parcelable: Android optimizes Parcelable for speed and memory usage, while Serializable is a Java standard that's slower and creates more temporary objects.
  • 保持Key一致: Make sure the string key you use to put and get the extra (like "CUSTOM_OBJECT_LIST_KEY") is identical in both Activities.
  • 处理大数据: If your ArrayList has hundreds or thousands of objects, avoid passing it via Intent—this can cause ANRs or crashes. Instead, use a ViewModel, singleton class, or local storage (like Room) to share the data.
  • Nested objects: If your custom class contains other objects, those objects must also implement Parcelable or Serializable.

内容的提问来源于stack exchange,提问作者Kazhiunea

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:30:02