android后台接口传参数,Android之旅十三 android中的数据传递方法

本文介绍了Android中数据传递的多种方法,包括Intent携带简单数据、复杂数据如ArrayList、通过实现Serializable和Parcelable接口。重点讲解了Parcelable的实现细节,包括writeToParcel、describeContents和Creator接口,强调了数据读写的顺序一致性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

android开发中,我们的Activity之间总避免不了进行数据的传递,几种传递方式大致如下,各有各的用处:

1、Intent携带简单的数据

Intent intent=new Intent();

Bundle bundle=new Bundle();

bundle.putString("username","Mary");

bundle.putInt("age",23);

intent.putExtras(bundle);

参数接收:Bundle bundle=getIntent().getExtras();

String username=bundle.getString("username");

int age=bundle.getInt("age");

2、Intent携带例如ArrayList之类复杂的数据

注意:在传参数前,要用新增加一个List将对象包起来

初始化数据:

Map map=new HashMap();

map.put("aaa","hello");

map.put("bbb","world");

List> list=new ArrayList>();

list.add(map);

Intent intent=new Intent();

Bundle bundle=new Bundle();

//须定义一个list用于在bundle中传递需要传递的List,这个是必须要的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值