Android Intent传递List数据(或者对象)

29 篇文章 0 订阅
10 篇文章 0 订阅

Android Intent传递List数据(或者对象)

好了,思想+代码。

代码:

第一个Activity类:
 Intent intent=new Intent();
                Bundle bundle = new Bundle();
                bundle.putSerializable("classify",listpostion.toArray());
                intent.putExtras(bundle);
                Log.d("JunLog",listpostion.toArray()+"");
                intent.setClass(ChooseStoryClassifyActivity.this,PushStoryActivity.class);
                startActivity(intent);
第二个Activity类
Intent intent=getIntent();
        Object classifyObj=intent.getSerializableExtra("classify");
        Object[] classify=(Object[]) classifyObj;
        if (null!=classifyObj){
            for (int position=0;position<classify.length;position++){
                String classifyStr=(String) classify[position];
                Log.d("JunLog","。。。。。。。。。"+classifyStr);
                add_story_classify.setText(classifyStr);
            };
        }
List
private List<String> listpostion=new ArrayList<>();
自己添加数据进去即可。
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想在 Android 中使用 Intent 传递 List 对象,你需要确保该对象序列化,然后将 List 对象放入 Intent 的 extras 中。 首先,你需要确保 List 中的对象实现了 Serializable 或 Parcelable 接口。Parcelable 更高效,但是 Serializable 更容易实现。以下是一个实现 Serializable 接口的示例类: ``` public class MyObject implements Serializable { private int id; private String name; public MyObject(int id, String name) { this.id = id; this.name = name; } public int getId() { return id; } public String getName() { return name; } } ``` 然后,你可以将 List 对象放入 Intent 的 extras 中: ``` List<MyObject> myList = new ArrayList<>(); myList.add(new MyObject(1, "John")); myList.add(new MyObject(2, "Jane")); Intent intent = new Intent(this, MyActivity.class); intent.putExtra("myList", (Serializable) myList); startActivity(intent); ``` 最后,在接收 Intent 的 Activity 中,你可以从 extras 中获取 List 对象: ``` List<MyObject> myList = (List<MyObject>) getIntent().getSerializableExtra("myList"); ``` 如果你的对象实现了 Parcelable 接口,你可以使用以下代码将 List 对象放入 Intent 的 extras 中: ``` List<MyObject> myList = new ArrayList<>(); myList.add(new MyObject(1, "John")); myList.add(new MyObject(2, "Jane")); Intent intent = new Intent(this, MyActivity.class); intent.putParcelableArrayListExtra("myList", (ArrayList<MyObject>) myList); startActivity(intent); ``` 最后,在接收 Intent 的 Activity 中,你可以从 extras 中获取 List 对象: ``` List<MyObject> myList = getIntent().getParcelableArrayListExtra("myList"); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值