我如何通过一个自定义类型的对象从一个活动到另一个使用类意图的putExtra()方法?


当前回答

如果你的对象类实现了Serializable,你不需要做任何其他事情,你可以传递一个Serializable对象。这就是我用的。

其他回答

首先在类中实现Parcelable。然后像这样传递object。

SendActivity.java

ObjectA obj = new ObjectA();

// Set values etc.

Intent i = new Intent(this, MyActivity.class);
i.putExtra("com.package.ObjectA", obj);

startActivity(i);

ReceiveActivity.java

Bundle b = getIntent().getExtras();
ObjectA obj = b.getParcelable("com.package.ObjectA");

包字符串不是必须的,只是在两个活动中字符串需要相同

参考

最简单的java方法是:在你的pojo/model类中实现serializable

推荐用于Android的性能视图:使模型可封装

对于你知道要在应用程序中传递数据的情况,使用“全局变量”(比如静态类)

以下是Dianne Hackborn (hackbod -谷歌安卓软件工程师)对此事的看法:

For situations where you know the activities are running in the same process, you can just share data through globals. For example, you could have a global HashMap<String, WeakReference<MyInterpreterState>> and when you make a new MyInterpreterState come up with a unique name for it and put it in the hash map; to send that state to another activity, simply put the unique name into the hash map and when the second activity is started it can retrieve the MyInterpreterState from the hash map with the name it receives.

有几种方法可以访问其他类或Activity中的变量或对象。

答:数据库

B.共同的偏好。

C.对象序列化。

D.可以保存公共数据的类可以命名为common Utilities,这取决于你。

E.通过intent和Parcelable接口传递数据。

这取决于您的项目需求。

答:数据库

SQLite是一个嵌入Android的开源数据库。SQLite支持标准的关系数据库特性,如SQL语法、事务和准备好的语句。

教程——http://www.vogella.com/articles/AndroidSQLite/article.html

B.共享偏好

假设您想存储用户名。现在有两个东西一个键用户名,值值。

如何储存

 // Create an object of SharedPreferences.
 SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
 //now get Editor
 SharedPreferences.Editor editor = sharedPref.edit();
 //put your value
 editor.putString("userName", "stackoverlow");

 //commits your edits
 editor.commit();

使用putString(),putBoolean(),putInt(),putFloat(),putLong(),您可以保存所需的数据类型。

如何获取

SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
String userName = sharedPref.getString("userName", "Not Available");

http://developer.android.com/reference/android/content/SharedPreferences.html

C.对象序列化

如果我们想要保存对象状态以便通过网络发送它,则使用对象序列化,或者您也可以将其用于您的目的。

使用java bean并将其存储为他的字段之一,并为此使用getter和setter

JavaBeans是具有属性的Java类。想到的 属性作为私有实例变量。因为它们是私人的,唯一的办法 可以通过类中的方法从类外部访问它们。的 改变属性值的方法称为setter方法,而这些方法称为setter方法 检索属性值的方法称为getter方法。

public class VariableStorage implements Serializable  {

    private String inString ;

    public String getInString() {
        return inString;
    }

    public void setInString(String inString) {
        this.inString = inString;
    }


}

在邮件方法中通过使用设置该变量

VariableStorage variableStorage = new VariableStorage();
variableStorage.setInString(inString);

然后使用对象序列化序列化此对象,并在其他类中反序列化此对象。

在序列化中,对象可以表示为字节序列,其中包括对象的数据、关于对象类型的信息以及存储在对象中的数据类型。

序列化对象被写入文件后,可以从文件中读取并反序列化它,也就是说,可以使用表示对象及其数据的类型信息和字节在内存中重新创建对象。

如果你想要这方面的教程,请参考这个链接

http://javawithswaranga.blogspot.in/2011/08/serialization-in-java.html

在其他类中获取变量

d . CommonUtilities

你可以自己创建一个类,它可以包含你在项目中经常需要的公共数据。

样本

public class CommonUtilities {

    public static String className = "CommonUtilities";

}

E.通过intent传递数据

有关传递数据的选项,请参阅本教程。

http://shri.blog.kraya.co.uk/2010/04/26/android-parcel-data-to-pass-between-activities-using-parcelable-classes/

如果你的对象类实现了Serializable,你不需要做任何其他事情,你可以传递一个Serializable对象。这就是我用的。