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


当前回答

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

以下是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.

其他回答

你可以通过intent发送可序列化对象

// send where details is object
ClassName details = new ClassName();
Intent i = new Intent(context, EditActivity.class);
i.putExtra("Editing", details);
startActivity(i);


//receive
ClassName model = (ClassName) getIntent().getSerializableExtra("Editing");

And 

Class ClassName implements Serializable {
} 

有几种方法可以访问其他类或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或Parcelable。

public class MY_CLASS implements Serializable

一旦完成,你可以在putExtra上发送一个对象

intent.putExtra("KEY", MY_CLASS_instance);

startActivity(intent);

要得到额外的,你只需要做

Intent intent = getIntent();
MY_CLASS class = (MY_CLASS) intent.getExtras().getSerializable("KEY");

如果您的类实现了Parcelable,请使用下一个

MY_CLASS class = (MY_CLASS) intent.getExtras().getParcelable("KEY");

我希望它能帮到你

Intent i = new Intent();
i.putExtra("name_of_extra", myParcelableObject);
startACtivity(i);

我使用Gson及其强大而简单的api在活动之间发送对象,

例子

// This is the object to be sent, can be any object
public class AndroidPacket {

    public String CustomerName;

   //constructor
   public AndroidPacket(String cName){
       CustomerName = cName;
   }   
   // other fields ....


    // You can add those functions as LiveTemplate !
    public String toJson() {
        Gson gson = new Gson();
        return gson.toJson(this);
    }

    public static AndroidPacket fromJson(String json) {
        Gson gson = new Gson();
        return gson.fromJson(json, AndroidPacket.class);
    }
}

2个函数,你将它们添加到你想要发送的对象中

使用

将对象从A发送到B

    // Convert the object to string using Gson
    AndroidPacket androidPacket = new AndroidPacket("Ahmad");
    String objAsJson = androidPacket.toJson();

    Intent intent = new Intent(A.this, B.class);
    intent.putExtra("my_obj", objAsJson);
    startActivity(intent);

在B接收

@Override
protected void onCreate(Bundle savedInstanceState) {        
    Bundle bundle = getIntent().getExtras();
    String objAsJson = bundle.getString("my_obj");
    AndroidPacket androidPacket = AndroidPacket.fromJson(objAsJson);

    // Here you can use your Object
    Log.d("Gson", androidPacket.CustomerName);
}

我几乎在我做的每个项目中都使用它,我没有任何性能问题。