通过序列化对象throught意图 [英] Pass serializable object throught intent

查看:131
本文介绍了通过序列化对象throught意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次问一个解决我的问题所以尽量善待人企图!我发现很多的解决方案,我在这个网站做简单的搜索处理的问题,但这个时候,我没有运气,我猜。我什么也没有发现适合我的问题,所以我最后问一个新问题。

This is my first attempt to ask for a solution to a problem of mine so try to be kind guys! I have found numerous solutions to problems I deal with in this site by doing simple search but this time I had no luck I guess. I found nothing to fit my problem so I ended asking a new question.

我是想通过一个意图从一个活动传递一个对象到另一个。我有一个抽象类和两个子类(其中一个也是抽象的),如下所示。我需要他们的抽象。我删除了很多code(抽象方法等)只是为了找到问题的是什么,我结束了这些非常简单的类,我仍然有同样的问题。

I 'm trying to pass an object through an intent from one activity to another. I have one abstract class and two subclasses (the one of them also abstract) as follows. I need them to be abstract. I removed a lot of code (abstract methods etc.) just to find what the problem is and I ended up with these really simple classes and I still have the same problem.

public abstract class MyObject {

    private boolean New;
    private boolean Modified;

    public MyObject() {
        New = true;
    }

    public boolean isNew() {
        return New;
    }

    protected void setNew(boolean value) {
        New = value;
    }

    protected boolean isModified() {
        return Modified;
    }

    protected void setModified(boolean value) {
        Modified = value;
    }

}

public abstract class MyItemBase extends MyObject {

    private Long Id;
    private String Description;

    public MyItemBase() {
        super();
    }

    public Long getId() {
        return Id;
    }

    public void setId(Long id) {
        Id = id;
    }

    public String getDescription() {
        return Description;
    }

    public void setDescription(String description) {
        Description = description;
    }

}

public class MyItem extends MyItemBase implements Serializable {

    private static final long serialVersionUID = -2875901021692711701L;

    public MyItem() {
        super();
    }

}

我有两个活动,ActivityA用一个按钮来调用ActivityB如下:

I have two Activities, ActivityA with a button to call ActivityB as follows.

public class ActivityA extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);

        Button btn = (Button) findViewById(R.id.button);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(),
                        ActivityB.class);

                MyItem item = new MyItem();
                item.setId((long) 1);
                item.setDescription("test");

                intent.putExtra("MyItem", (Serializable) item);
                startActivityForResult(intent, 1);
            }

        });
    }

}

public class ActivityB extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_b);

        MyItem item = (MyItem) getIntent().getSerializableExtra("MyItem");
        ((TextView) findViewById(R.id.text)).setText(item.getDescription());
    }

}

所以,当我收到MyItem从第二个活动,没有错误可言,这两个属性标识和描述都为空。其实这是一个新的对象,因为新属性为true。现在,如果我尝试获取对象我把它的意图,像第一个活动后,究竟

So when I receive MyItem from the second activity, with no errors at all, both properties Id and Description are null. Actually it's a new object because the New property is true. Now if I try to get the object exactly after I put it in the intent in the first Activity like that

intent.putExtra("MyItem", (Serializable) item);
MyItem newItem = (MyItem) intent.getSerializableExtra("MyItem");
startActivityForResult(intent, 1);

它工作正常。的newitem拥有所有正确值的属性。

it works fine. newItem has all the properties with the correct values.

任何想法?

感谢名单提前大家!

推荐答案

从的官方Serializable接口文档

这不实现此接口不会有任何其状态的序列化或反序列化类。可序列化类的所有子类型本身都是可序列化的。

Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable.

您基类没有实现序列化,所以它的领域没有得到序列化。

Your base class doesn't implement Serializable, so its fields are not getting serialized.

MyItemBase (甚至为MyObject 序列化和其所有后代也将成为序列化(没有明确实现接口)。

Make MyItemBase (or even MyObject) Serializable and all of its descendants will also become Serializable (without explicitly implementing the interface).

这篇关于通过序列化对象throught意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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