我正在尝试从一个“活动”发送客户类的对象,并在另一个“”中显示它。

客户类别的代码:

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;
    }
}

我想将其对象从一个“活动”发送到另一个“,然后在另一个活动”上显示数据。

我怎样才能做到这一点?


当前回答

最好的方法是在应用程序中有一个类(称为Control),该类将保存类型为“Customer”的静态变量(在您的情况下)。初始化活动A中的变量。

例如:

Control.Customer = CustomerClass;

然后转到Activity B并从Control类获取它。使用变量后不要忘记赋值null,否则会浪费内存。

其他回答

在自定义类中创建两个方法,如下所示

public class Qabir {

    private int age;
    private String name;

    Qabir(){
    }

    Qabir(int age,String name){
        this.age=age; this.name=name;
    }   

    // method for sending object
    public String toJSON(){
        return "{age:" + age + ",name:\"" +name +"\"}";
    }

    // method for get back original object
    public void initilizeWithJSONString(String jsonString){

        JSONObject json;        
        try {
            json =new JSONObject(jsonString );
            age=json.getInt("age");
            name=json.getString("name");
        } catch (JSONException e) {
            e.printStackTrace();
        } 
    }
}

现在在您的发件人活动中这样做

Qabir q= new Qabir(22,"KQ");    
Intent in=new Intent(this,SubActivity.class);
in.putExtra("obj", q.toJSON());
startActivity( in);

在您的接收器中活动

Qabir q =new Qabir();
q.initilizeWithJSONString(getIntent().getStringExtra("obj"));

正如在评论中提到的,这个答案打破了封装并紧密耦合了组件,这很可能不是您想要的。最好的解决方案可能是使对象可Parcelable或Serializable,正如其他响应所解释的那样。话虽如此,解决方案解决了问题。所以,如果你知道你在做什么:

使用带有静态字段的类:

public class Globals {
    public static Customer customer = new Customer();
}

在活动内部,您可以使用:

活动来源:

Globals.customer = myCustomerFromActivity;

活动目标:

myCustomerTo = Globals.customer;

这是传递活动信息的简单方法。

从此活动启动另一个活动,并通过捆绑对象传递参数

Intent intent = new Intent(getBaseContext(), YourActivity.class);
intent.putExtra("USER_NAME", "xyz@gmail.com");
startActivity(intent);

检索其他活动的数据(YourActivity)

String s = getIntent().getStringExtra("USER_NAME");

这对于一种简单的数据类型是可以的。但是如果你想在活动之间传递复杂的数据。你需要先序列化它。

这里有员工模型

class Employee{
    private String empId;
    private int age;
    print Double salary;
    
    getters...
    setters...
}

您可以使用google提供的Gson-lib来序列化复杂的数据这样地

String strEmp = new Gson().toJson(emp);
Intent intent = new Intent(getBaseContext(), YourActivity.class);
intent.putExtra("EMP", strEmp);
startActivity(intent);

Bundle bundle = getIntent().getExtras();
String empStr = bundle.getString("EMP");
            Gson gson = new Gson();
            Type type = new TypeToken<Employee>() {
            }.getType();
            Employee selectedEmp = gson.fromJson(empStr, type);

一种选择是让自定义类实现Serializable接口,然后可以使用intent#putExtra()方法的putExtra变量(Serializable..)在intent extra中传递对象实例。

实际代码:

在自定义模型/对象类中:

public class YourClass implements Serializable {

在使用自定义模型/类的其他类中:

//To pass:
intent.putExtra("KEY_NAME", myObject);

myObject的类型为“YourClass”。然后,要从另一个活动中检索,请使用getSerializableExtra使用相同的Key名称获取对象。需要对YourClass进行类型转换:

// To retrieve object in second Activity
myObject = (YourClass) getIntent().getSerializableExtra("KEY_NAME");

注意:确保主自定义类的每个嵌套类都实现了Serializable接口,以避免任何序列化异常。例如:

class MainClass implements Serializable {
    
    public MainClass() {}

    public static class ChildClass implements Serializable {
         
        public ChildClass() {}
    }
}

使用Serializable实现类。假设这是您的实体类:

import java.io.Serializable;

@SuppressWarnings("serial") //With this annotation we are going to hide compiler warnings
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;
}

我们正在将名为dene的对象从X活动发送到Y活动。在X活动的某处;

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

在Y活动中,我们正在获取对象。

Intent i = getIntent();
Deneme dene = (Deneme)i.getSerializableExtra("sampleObject");

就是这样。