我正在尝试从一个“活动”发送客户类的对象,并在另一个“”中显示它。
客户类别的代码:
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;
}
}
我想将其对象从一个“活动”发送到另一个“,然后在另一个活动”上显示数据。
我怎样才能做到这一点?
您还可以将对象的数据写入临时字符串和int,并将它们传递给活动。当然,这样可以得到传输的数据,但不能得到对象本身。
但是,如果您只想显示它们,而不在其他方法或类似方法中使用对象,那么就足够了。我以同样的方式在另一个活动中显示一个对象的数据。
String fName_temp = yourObject.getFname();
String lName_temp = yourObject.getLname();
String age_temp = yourObject.getAge();
String address_temp = yourObject.getAddress();
Intent i = new Intent(this, ToClass.class);
i.putExtra("fname", fName_temp);
i.putExtra("lname", lName_temp);
i.putExtra("age", age_temp);
i.putExtra("address", address_temp);
startActivity(i);
你也可以直接传递它们,而不是传递临时变量,但在我看来,这样更清楚。此外,您可以将temp ivars设置为null,以便GarbageCollector更快地清理它们。
祝你好运
附带说明:重写toString()而不是编写自己的打印方法。
正如下面的评论中提到的,这是您在另一个活动中获取数据的方式:
String fName = getIntent().getExtras().getInt("fname");
我知道静态是不好的,但似乎我们不得不在这里使用它。parceables/seriazables的问题是两个活动具有相同对象的重复实例=内存和CPU的浪费。公共类IntentMailBox{静态队列<对象>content=newLinkedList<对象>();}
呼叫活动
IntentMailBox.content.add(level);
Intent intent = new Intent(LevelsActivity.this, LevelActivity.class);
startActivity(intent);
调用的活动(请注意,当系统销毁和重新创建活动时,可能会多次调用onCreate()和onResume())
if (IntentMailBox.content.size()>0)
level = (Level) IntentMailBox.content.poll();
else
// Here you reload what you have saved in onPause()
另一种方法是声明要在该类中传递的类的静态字段。它仅用于此目的。不要忘记,在onCreate中它可以为空,因为系统已经从内存中卸载了应用程序包,并在稍后重新加载。请记住,您仍然需要处理活动生命周期,您可能希望将所有数据直接写入共享的首选项,这对于复杂的数据结构来说是很痛苦的。
创建自己的类Customer,如下所示:
import import java.io.Serializable;
public class Customer implements Serializable
{
private String name;
private String city;
public Customer()
{
}
public Customer(String name, String city)
{
this.name= name;
this.city=city;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getCity()
{
return city;
}
public void setCity(String city)
{
this.city= city;
}
}
在onCreate()方法中
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_top);
Customer cust=new Customer();
cust.setName("abc");
cust.setCity("xyz");
Intent intent=new Intent(abc.this,xyz.class);
intent.putExtra("bundle",cust);
startActivity(intent);
}
在xyz活动类中,您需要使用以下代码:
Intent intent=getIntent();
Customer cust=(Customer)intent.getSerializableExtra("bundle");
textViewName.setText(cust.getName());
textViewCity.setText(cust.getCity());
大家好,我看到了很多好的选项,但我想知道为什么没有使用绑定?
对我来说,传递对对象的引用似乎比序列化和反序列化对象更有效,但我还没有深入了解这是否是幕后发生的事情。
创建活页夹非常简单。。。
public class MyBinder extends Binder {
private Object myObject;
public MyBinder(Object object) {
myObject = object;
}
public Object getObject() {
return myObject;
}
}
创造出可以使用它的parcelable并不是那么糟糕的乙醚。
public class MyParcelable implements Parcelable {
private Object myObject;
public MyParcelable() {
}
public MyParcelable(Parcel parcel) {
myObject = ((MyBinder)parcel.readStrongBinder()).getObject();
}
public void setObject(Object object) {
myObject = object;
}
public Object getObject() {
return myObject;
}
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeStrongBinder(new MyBinder(myObject));
}
public int describeContents() {
return myObject == null ? 0 : 1;
}
public static final Parcelable.Creator<MyParcelable> CREATOR = new Parcelable.Creator<MyParcelable>() {
public MyParcelable createFromParcel(Parcel parcel) {
return new MyParcelable(parcel);
}
public MyParcelable[] newArray(int length) {
return new MyParcelable[length];
}
};
}
这个逻辑非常酷,因为实际上是在将引用从一个活动传递到另一个活动。
我建议检查空值,如果Binder的实例是MyBinder!
为了实现这一点,你。。。
把它送出去
Object myObject = "some object";
MyParcelable myParcelable = new MyParcelable();
myParcelable.setObject(myObject);
intent.putExtra("MyParcelable", myParcelable);
把它拿回来
myParcelable = (MyParcelable) getIntent().getExtras().getParcelable("MyParcelable");
myObject = myParcelable.getObject();
见鬼,有人会变得疯狂,让这个笨蛋成为真正的普通人。
这个问题也在另一个堆栈溢出问题中讨论。请查看使用Serializable通过意向传递数据的解决方案。重点是使用Bundle对象,它在Intent中存储必要的数据。
Bundle bundle = new Bundle();
bundle.putSerializable(key1, value1);
bundle.putSerializable(key2, value2);
bundle.putSerializable(key3, value3);
intent.putExtras(bundle);
要提取值,请执行以下操作:
Bundle bundle = new Bundle();
for (String key : bundle.keySet()) {
value = bundle.getSerializable(key));
}
Serializable的优点是简单。然而,如果需要传输许多数据,您应该考虑使用Parcelable方法,因为Parcelable是专门为Android设计的,它比Serializable更高效。可以使用以下命令创建Parcelable类:
在线工具parcelablerAndroid Studio插件-Android Parcelable代码生成器
根据我的经验,有三种主要的解决方案,各有其缺点和优点:
实施Parcelable实现可串行化使用某种轻量级事件总线库(例如,Greenrobot的EventBus或Square的Otto)
Parcelable-快速且符合Android标准,但它有很多样板代码,并且需要硬编码字符串以供在提取意图值(非强类型)时参考。
可序列化-接近于零样板,但这是最慢的方法,并且在从意图中提取值(非强类型)时还需要硬编码字符串。
事件总线-零样板,最快的方法,不需要硬编码字符串,但它需要额外的依赖项(虽然通常很轻,约40 KB)
我发布了这三种方法的非常详细的比较,包括效率基准。