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


当前回答

使用谷歌的Gson库,您可以将object传递给另一个活动。实际上,我们将以json字符串的形式转换对象,传递给其他活动后,我们将再次重新转换为这样的对象

考虑这样一个bean类

 public class Example {
    private int id;
    private String name;

    public Example(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

我们需要传递Example类的对象

Example exampleObject=new Example(1,"hello");
String jsonString = new Gson().toJson(exampleObject);
Intent nextIntent=new Intent(this,NextActivity.class);
nextIntent.putExtra("example",jsonString );
startActivity(nextIntent);

对于读取,我们需要在NextActivity中做相反的操作

 Example defObject=new Example(-1,null);
    //default value to return when example is not available
    String defValue= new Gson().toJson(defObject);
    String jsonString=getIntent().getExtras().getString("example",defValue);
    //passed example object
    Example exampleObject=new Gson().fromJson(jsonString,Example .class);

在gradle中添加这个依赖

compile 'com.google.code.gson:gson:2.6.2'

其他回答

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

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

我知道有点晚了,但如果你只想对一些对象这么做为什么不直接在目标活动中将对象声明为公共静态对象呢?

public static myObject = new myObject();

从你的源活动中给它一个值?

destinationActivity.myObject = this.myObject;

在您的源活动中,您可以像使用任何全局对象一样使用它。 对于大量的对象,这可能会导致一些内存问题,但对于少数对象,我认为这是最好的方法

在臂膀上

在build.gradle中添加kotlin扩展。

apply plugin: 'kotlin-android-extensions'

android {
    androidExtensions {
        experimental = true
   }
}

然后像这样创建数据类。

@Parcelize
data class Sample(val id: Int, val name: String) : Parcelable

传递带有意图的对象

val sample = Sample(1,"naveen")

val intent = Intent(context, YourActivity::class.java)
    intent.putExtra("id", sample)
    startActivity(intent)

获取带有意图的对象

val sample = intent.getParcelableExtra("id")

我知道有点晚了,但很简单。你所要做的就是让你的类实现Serializable

public class MyClass implements Serializable{

}

然后你可以传递给一个意图,比如

Intent intent=......
MyClass obje=new MyClass();
intent.putExtra("someStringHere",obje);

要得到它,你只需打电话

MyClass objec=(MyClass)intent.getExtra("theString");