Android:Bundle传递数据和对象

1、Bundle传递数据, 因为Bundle中已经封装好了简单数据类型,所以我们直接去设置数据,下面就来看看具体的操作:

  case R.id.Btn_Msg:
                // 实例化一个Bundle  
                Bundle bundle = new Bundle();
                Intent intent=new Intent(MainActivity.this,Main2Activity.class);
                //设置数据
                String name="admin";String num="123";
                //把数据保存到Bundle里  
                bundle.putString("name", name);
                bundle.putString("num",num);
                //把bundle放入intent里  
                intent.putExtra("Message",bundle);
                startActivity(intent);
                break;
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

把数据放到Bundle中,然后仔使用intent去传递,下面再来看下怎么去获取的从Bundle中传递的数据:

 //获取数据  
        Intent intent = getIntent();
        //从intent取出bundle  
        Bundle bundle = intent.getBundleExtra("Message");
        //获取数据  
        String name = bundle.getString("name");
        String num = bundle.getString("num");
        //显示数据  
        text_show.setText(name + "\n" + num);
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

2、Bundle传递对象,如果我们想要传递一个复杂的数据类型就会用到Bundle中的方法Serizlizable
在这里我们要把数据转成Serizlizable对象,然后在进行相应的操作

如这样的一个对象:

public class Persion implements Serializable {
    private String name;
    private String num;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNum() {
        return num;
    }

    public void setNum(String num) {
        this.num = num;
    }
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

具体操作如下,这边把数据设置写死了,,开始传递对象

  case R.id.Btn_Obj:
                Persion persion=new Persion();
                //设置数据
                String Name="zhangsan";String Num="111111";
                persion.setName(Name);
                persion.setNum(Num);
                // 实例化一个Bundle  
                Bundle bundle1 = new Bundle();
                // 把Persion数据放入到bundle中  
                bundle1.putSerializable("persion",persion);
                Intent intent1=new Intent(MainActivity.this,Main2Activity.class);
                intent1.putExtras(bundle1);
                startActivity(intent1);
                break;
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

同理我也是把对象放到Bundle中,然后在使用Intent进行传递,下面看看我们怎么获取Bundle传递的对象:

 Intent intent=getIntent();
        // 实例化一个Bundle  
        Bundle bundle=intent.getExtras();
        //获取里面的Persion里面的数据  
        Persion persion= (Persion) bundle.getSerializable("persion");
        text_show.setText("姓名:"+persion.getName()+"\n"+"号码:"+persion.getNum());
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

总结一下,在使用Bundle传递对象的时候,我先让Persion类实现Serializable接口,然后用putSerializable(String key,Serializble value)来存储数据,接收数据的时候再Serializanle getSerizlizble(String key)来取出数据,,道理都很简单!只是需要一步一步的去解决,,,,做人和写代码一样,都是需要一步一步脚踏实地的去做,没有什么一步登天。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值