如何传递 ArrayList<Custom_Object>在 Android 中从一项活动到另一项活动? [英] How to pass ArrayList<Custom_Object> from one activity to another in Android?
问题描述
我正在尝试将自定义对象从一个活动发送到另一个活动,但是当我调用开始活动时它崩溃了.
I am trying to send a custom object from one activity to another activity, but it's crashing when I call the start activity.
以下是我使用的片段.
我的活动实现了Serializable
ArrayList<CUSTOM_OBJECT> Cus_Obje_arraylist = new ArrayList<CUSTOM_OBJECT>();
这是我的意图:
Intent inte = new Intent(getApplicationContext(), ListActivity.class); `
inte.putExtra("list",Cus_Obje_arraylist);`
startActivity(inte);
请告诉我它为什么会崩溃或者我可以使用什么替代方法?
Please let me know why it's crashing or what alternate way I can use?
推荐答案
我可以给个建议.我在我的项目中这样做.
I can give a suggestion. I do this in my project.
1.实现一个单例类作为传递对象的桥梁.(希望你知道什么是单身,我你不知道,添加评论告诉我.
1.Implement a singleton class as the bridge to pass object. (Hopefully you know what's singleton, I you don't, add comment to tell me.
class BridgeClass {
private BridgeClass() {}
static BridgeClass obj = nil;
public BridgeClass instance() {
if (obj == nil) obj = new BridgeClass();
return obj;
}
public ArrayList<CUSTOM_OBJECT> cache;
}
2.在 from 活动中,
2.In the from activity,
BridgeClass.instance().cache = Cus_Obje_arraylist;
3.然后在to活动中,可以从bridge类中获取.
3.Then in the to activity, you can get it from the bridge class.
ArrayList<CUSTOM_OBJECT> Cus_Obje_arraylist = BridgeClass.instance().cache;
这篇关于如何传递 ArrayList<Custom_Object>在 Android 中从一项活动到另一项活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!