亲宝软件园·资讯

展开

Android Intent跳转传递list集合

Activity_ 人气:0

引言

传递list集合

注意: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集合

传递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");
//接收到一整个实体类

传递String

 Intent intent = new Intent(this,MainActivity2.class);
 //设置传递键值对
 intent.putExtra("name","花花");
 //激活意图
 startActivity(intent);
 Intent intent = getIntent();
 //获取传递的值
 String name= intent.getStringExtra("name");
 //name的值是:花花
 

加载全部内容

相关教程
猜你喜欢
用户评论