2 xiaoxiaoaishang xiaoxiaoaishang 于 2012.09.25 14:16 提问

在android中,如何从一个activity向另外一个activity传递object?

我正在努力尝试从一个activity中发送我的自定义类中的一个object,然后显示在另外一个activity中
自定义类的代码:

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;

    }

}

我想要从一个activity传递object到另外一个activity然后在另外一个activity现实数据。
我怎么能够实现?

7个回答

yizhenbeifeng
yizhenbeifeng   2012.09.25 15:15
已采纳

一个可能的方法是让你的自定义类实现Serializable接口,你可以用putExtra(Serializable..)通过Intent#putExtra() 方法传递对象实例
PSEUDO代码:

   intent.putExtra("MyClass", obj);  

getIntent().getSerializableExtra("MyClass");
scliu0718
scliu0718   2012.09.26 09:46

果然无所不用啊。 该用的方法能用的方法大家都写出来了。果然三人行必有我师!
上面的两种方法是常用的方法(在开发中),另外我说一种方法可用,但也许不是那么的实际,效率不是那么高。你可以做一个自定义类型的全局变量,在StartActivity前存,在第二个Activity的onCreate方法中去取出全局变量的值。

好的,拿上来了.

public class CanvasGlobelManager {

private static CanvasGlobelManager instance = null;
private int colorsSelectedItem = 0;
static {
    instance = new CanvasGlobelManager();
}
private CanvasGlobelManager() {
}
public static CanvasGlobelManager getInstanse() {
    return instance;
}
public synchronized int getColorsSelectedItem() {
    return this.colorsSelectedItem;
}
public synchronized void setColorsSelectedItem(int colorsSelected) {
    this.colorsSelectedItem = colorsSelected;
}
public synchronized void resetColorsSelectedItem() {
    this.colorsSelectedItem = 0;
}

}

yiranwujixian
yiranwujixian 非常感谢,我去试试!
接近 5 年之前 回复
suannai0314
suannai0314 你可以编辑之前的回答,然后把代码贴到回答中
接近 5 年之前 回复
scliu0718
scliu0718 public class CanvasGlobelManager { private static CanvasGlobelManager instance = null; private int colorsSelectedItem = 0; static { instance = new CanvasGlobelManager(); } private CanvasGlobelManager() { } public static CanvasGlobelManager getInstanse() { return instance; } public synchronized int getColorsSelectedItem() { return this.colorsSelectedItem; } public synchronized void setColorsSelectedItem(int colorsSelected) { this.colorsSelectedItem = colorsSelected; } }
接近 5 年之前 回复
scliu0718
scliu0718 在跳转前setColorsSelectedItem(1);跳转后getColorsSelectedItem();数据类型当然可以自定义,这只是一个我用到的例子
接近 5 年之前 回复
yiranwujixian
yiranwujixian 你这个能不能贴下代码让我参考下?谢谢啦
接近 5 年之前 回复
franzhong
franzhong   2012.09.25 15:19
Intent intent = new Intent();
intent.setClass(MyActivity.this,anotherActivity.class);

Bundle bundle = new Bundle();
bundle.putStringArray("MYARR", myArr);//array object
bundle.putString("TYPE", type);//string 
intent.putExtras(bundle);
startActivity(intent);
//this .finished();

类似以上方法

yongyong_21
yongyong_21 要编辑代码格式(⊙o⊙)哦。
大约 5 年之前 回复
Sueyexin
Sueyexin   2012.09.25 15:49

用Serializable实现你的类。让我们假设这是你的实体类

import java.io.Serializable;

@SuppressWarnings("serial")
public class Deneme implements Serializable {

public Deneme(double id, String name){
    this.id = id;
    this.name = name;
}

public double getId() {
    return id;
}
public void setId(double id) {
    this.id = id;
}
public String getName() {
    return this.name;
}
public void setName(String name) {
    this.name = name;
}

private double id;
private String name;

}

我从Xactivity向Y activity传递这个叫dene的对象。

Deneme dene = new Deneme(4,"Mustafa");
Intent i = new Intent(this, Y.class);
i.putExtra("sampleObject", dene);
startActivity(i);

在Y activity中我获得这个对象

Intent i = getIntent();
Deneme dene = (Deneme)i.getSerializableExtra("sampleObject");
mansuifengqi
mansuifengqi   2012.09.25 16:27

当调用一个activity

Intent intent = new Intent(fromClass.this,toClass.class).putExtra("myCustomerObj",customerObj);

在toClass.java 接收activity:

Customer customerObjInToClass = getIntent().getExtras().getParcelable("myCustomerObj");

确保自定义类实现 parcelable

 public class Customer implements Parcelable {

    private String firstName, lastName, Address;
        int Age;

    public Customer (Parcel in) {
            readFromParcel(in);
        }

        public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
            public LeadData createFromParcel(Parcel in) {
                return new Customer (in);
            }

            public Customer [] newArray(int size) {
                return new Customer [size];
            }
        };

    @Override
        public void writeToParcel(Parcel dest, int flags) {

            dest.writeString(firstName);
            dest.writeString(lastName);
                    dest.writeString(Address);
            dest.writeInt(Age);
    }

        private void readFromParcel(Parcel in) {

            firstName= in.readString();
            lastName= in.readString();
            Address= in.readString();
            Age= in.readInt();
gary16
gary16   2016.02.03 15:01

****使用bundle类

oSanYeCao1234567
oSanYeCao1234567   Ds   Rxr 2016.10.10 23:02
Csdn user default icon
上传中...
上传图片
插入图片