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


当前回答

Start another activity from this activity pass parameters via Bundle Object

Intent intent = new Intent(this, YourActivity.class);
Intent.putExtra(AppConstants.EXTRAS.MODEL, cModel);
startActivity(intent);
Retrieve on another activity (YourActivity)

ContentResultData cModel = getIntent().getParcelableExtra(AppConstants.EXTRAS.MODEL);

其他回答

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

如果你对putExtra特性的使用不是很特别,只是想启动另一个带有对象的活动,你可以查看我写的GNLauncher (https://github.com/noxiouswinter/gnlib_android/wiki#gnlauncher)库,它试图使这个过程更直接。

GNLauncher可以将对象/数据从另一个活动发送到一个活动,就像调用活动中的函数一样简单,并将所需的数据作为参数。它引入了类型安全,并消除了必须序列化的所有麻烦,使用字符串键附加到意图,并在另一端撤销相同的操作。

最简单的方法是在item是字符串的情况下使用以下代码:

intent.putextra("selected_item",item)

接收:

String name = data.getStringExtra("selected_item");

你可以使用android BUNDLE来做到这一点。

从你的类中创建一个Bundle,像这样:

public Bundle toBundle() {
    Bundle b = new Bundle();
    b.putString("SomeKey", "SomeValue");

    return b;
}

然后用INTENT传递这个bundle。 现在你可以通过传递bundle来重新创建你的类对象

public CustomClass(Context _context, Bundle b) {
    context = _context;
    classMember = b.getString("SomeKey");
}

在自定义类中声明并使用。

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