如何传递一个对象从一个活动到另一个机器人
问题描述:
我想对发送的对象我的客户的从一个活动类
工作,并在另一个显示它活动
。
I am trying to work on sending an object of my customer class from one Activity
and display it in another Activity
.
在code为客户类:
public class Customer {
private String firstName, lastName, Address;
int Age;
public Customer(String fname, String lname, int age, String address) {
firstName = fname;
lastName = lname;
Age = age;
Address = address;
}
public String printValues() {
String data = null;
data = "First Name :" + firstName + " Last Name :" + lastName
+ " Age : " + Age + " Address : " + Address;
return data;
}
}
我想从一个活动
另活动
发送其对象到另一个,然后显示数据。
I want to send its object from one Activity
to another and then display the data on the other Activity
.
我怎样才能做到这一点?
How can I achieve that?
答
一个选项可以让你自定义的类实现序列化
接口,然后你可以通过在对象实例意图额外使用 putExtra(序列化。)
的意向#putExtra()
法的变种。
One option could be letting your custom class implement the Serializable
interface and then you can pass object instances in the intent extra using the putExtra(Serializable..)
variant of the Intent#putExtra()
method.
伪code
//To pass:
intent.putExtra("MyClass", obj);
// To retrieve object in second Activity
getIntent().getSerializableExtra("MyClass");