如何传递一个对象从一个活动到另一个机器人 [英] How to pass an object from one activity to another on Android

查看:110
本文介绍了如何传递一个对象从一个活动到另一个机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对发送的对象我的客户的从一个活动类工作,并在另一个显示它活动

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");

这篇关于如何传递一个对象从一个活动到另一个机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆