gpt4 book ai didi

android - 使用 getIntent().getExtras() 获取 ArrayList

转载 作者:行者123 更新时间:2023-11-29 15:58:04 26 4
gpt4 key购买 nike

在我的应用程序的一个 Activity 中,我有一个启动新 Activity 的 Intent ,我想传递一个 ArrayList,即一个 ArrayList,其中每个项目都是一个自己的类的实例......我该怎么做?

我一直在寻找可能的 getExtras(),例如 getStringArrayList() 或 getParcelableArrayList(),但它不起作用,我还没有找到任何有效的类型。

谁能帮帮我?谢谢。

这是我的课:

public class ItemFile {
protected long id;
protected String nombre;
protected String rutaImagen;
protected boolean checked;

public ItemFile(long id, String nombre, String rutaImagen, boolean check) {
this.id = id;
this.nombre = nombre;
this.rutaImagen = rutaImagen;
this.checked = check;
}

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getNombre() {
return nombre;
}

public void setNombre(String nombre) {
this.nombre = nombre;
}

public String getRutaImagen() {
return rutaImagen;
}

public void setRutaImagen(String rutaImagen) {
this.rutaImagen = rutaImagen;
}

public boolean isChecked() {
return checked;
}

public void setChecked(boolean checked){
this.checked = checked;
}

我必须如何更改它才能设置 Parcelable?

最佳答案

首先确保 MyClass 类实现了 Parcelable 接口(interface),然后使用此代码在其他 Activity 中获取 ArrayList:

在你的第一个 Activity 中做这样的事情:

ArrayList<MyClass> myClassList= new ArrayList<MyClass>();
Intent intent = new Intent(<YOUR ACTIVITY CONTEX>, <NEXT ACTIVITY>);
intent.putExtra("myClassList", myClassList);
startActivity(intent);

在你的下一个 Activity 中做这样的事情:

ArrayList<MyClass> list = (ArrayList<MyClass>)getIntent().getExtras()getSerializable("myClassList");

关于android - 使用 getIntent().getExtras() 获取 ArrayList<MyClass>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26779571/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com