Android怎么传递类对象,通过 Intent 传递类对象

通过 Intent 传递类对象

Android中Intent传递类对象提供了两种方式一种是 通过实现Serializable接口传递对象,一种是通过实现Parcelable接口传递对象。

要求被传递的对象必须实现上述2种接口中的一种才能通过Intent直接传递

Intent中传递这2种对象的方法:

Bundle.putSerializable(Key,Object); //实现Serializable接口的对象

Bundle.putParcelable(Key, Object); //实现Parcelable接口的对象

Person.java

package com.hust.bundletest;

import java.io.Serializable;

public class Person implements Serializable {

String name;

String password;

String sex;

public Person(String name, String password, String sex) {

super();

this.name = name;

this.password = password;

this.sex = sex;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public String getSex() {

return sex;

}

public void setSex(String sex) {

this.sex = sex;

}

}

注册窗体发送Intent的代码:

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button btn=(Button) findViewById(R.id.button1);

btn.setOnClickListener(new OnClickListener(){

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

//获得的三个组件

EditText name=(EditText) findViewById(R.id.name);

EditText password=(EditText) findViewById(R.id.password);

RadioButton male=(RadioButton) findViewById(R.id.radio0);

//判断是否被选

String sex=(male.isChecked())?"男":"女";

//封装成一个对象

Person p=new Person(name.getText().toString(),password.getText().toString(),sex);

//创建Bundle对象

Bundle bundle=new Bundle();

bundle.putSerializable("person", p);//Bundle中放一个对象

//创建一个Intent对象

Intent intent=new Intent(MainActivity.this,ResultActivity.class);

intent.putExtras(bundle);

//启动intent对应的Activity

startActivity(intent);

}

});

接收端的代码:

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_result);

//获取显示组件

TextView name=(TextView) findViewById(R.id.text1);

TextView password=(TextView) findViewById(R.id.text2);

TextView sex=(TextView) findViewById(R.id.text3);

//获取Intent对象

Intent intent=getIntent();

//从Intent对象中获取序列数据

//Person p=(Person) intent.getSerializableExtra("person");相当于

Bundle bundle=intent.getExtras();//获取Bundle对象

Person p=(Person) bundle.getSerializable("person");//Bundle对象中获取可序列化对象

name.setText(p.getName());

password.setText(p.getPassword());

sex.setText(p.getSex());

}

以上就可以实现对象的传递。

如果传递的是List,可以把list强转成Serializable类型,而且object类型也必须实现了Serializable接口

Intent.putExtras(key, (Serializable)list)

(List)getIntent().getSerializable(key)

http://www.dengb.com/Androidjc/1107233.htmlwww.dengb.comtruehttp://www.dengb.com/Androidjc/1107233.htmlTechArticle通过 Intent 传递类对象 Android中Intent传递类对象提供了两种方式一种是 通过实现Serializable接口传递对象,一种是通过实现Parcelable接口传递对...

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值