目录引言传递list集合传递ArrayList集合传递实体类传递String引言 Android 两个activity之间普通传值 如:单个String ,int ... 就不多说了
注意:list传值的实体类用序列化
import java.io.Serializable;
//别忘了序列化 Serializable
public class DemoBean implements Serializable {
String xm;
int age;
public String getXm() {
return xm;
}
public void setXm(String xm) {
this.xm = xm;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
//写个测试方法
public void ToJump(){
List<DemoBean> list=new ArrayList<>();
//给list 添加 5 条数据
for (int i=0;i<5;i++){
DemoBean demoBean=new DemoBean();
demoBean.setXm("花花"+i);
demoBean.setAge(i);
list.add(demoBean);
}
Intent intent=new Intent(this,MainActivity.class);
intent.putExtra("list", (Serializable) list);
startActivity(intent);
}
Intent intent=getIntent();
List<DemoBean> list= (List<DemoBean>) intent.getSerializableExtra("list");
Log.e("TAG","---"+list.size());// list.size()==5
传递ArrayList集合和list一样
注意:实体类用序列化
public void ToJump(){
ArrayList<DemoBean> arrayList=new ArrayList();
for (int i=0;i<5;i++){
DemoBean demoBean=new DemoBean();
demoBean.setXm("花花"+i);
demoBean.setAge(i);
arrayList.add(demoBean);
}
Intent intent=new Intent(this,MainActivity.class);
intent.putExtra("arrayList",arrayList);
startActivity(intent);
}
Intent intent=getIntent();
ArrayList<DemoBean> arrayList= (ArrayList<DemoBean>) intent.getSerializableExtra("arrayList");
Log.e("TAG","--------"+arrayList.size());// 有5条数据 list.size()==5
public void ToJump(){
DemoBean demoBean=new DemoBean();
Intent intent=new Intent(this,MainActivity.class);
intent.putExtra("demoBean",demoBean);
startActivity(intent);
}
Intent intent=getIntent();
DemoBean demoBean= (DemoBean) intent.getSerializableExtra("demoBean");
//接收到一整个实体类
Intent intent = new Intent(this,MainActivity2.class);
//设置传递键值对
intent.putExtra("name","花花");
//激活意图
startActivity(intent);
Intent intent = getIntent();
//获取传递的值
String name= intent.getStringExtra("name");
//name的值是:花花
以上就是Android开发Intent跳转传递list集合实现示例的详细内容,更多关于Android Intent跳转传递list集合的资料请关注编程网其它相关文章!
--结束END--
本文标题: Android开发Intent跳转传递list集合实现示例
本文链接: https://lsjlt.com/news/165504.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0