我正在尝试从一个“活动”发送客户类的对象,并在另一个“”中显示它。

客户类别的代码:

public class Customer {

    private String firstName, lastName, address;
    int age;

    public Customer(String fname, String lname, int age, String address) {

        firstName = fname;
        lastName = lname;
        age = age;
        address = address;
    }

    public String printValues() {

        String data = null;

        data = "First Name :" + firstName + " Last Name :" + lastName
        + " Age : " + age + " Address : " + address;

        return data;
    }
}

我想将其对象从一个“活动”发送到另一个“,然后在另一个活动”上显示数据。

我怎样才能做到这一点?


当前回答

将对象从一个活动传递到另一个活动。

(1) 源放射性

Intent ii = new Intent(examreport_select.this,
                    BarChartActivity.class);

            ii.putExtra("IntentExamResultDetail",
                    (Serializable) your List<ArraList<String>> object here);
            startActivity(ii);

(2) 目的地活动性

List<ArrayList<String>> aa = (List<ArrayList<String>>) getIntent()
            .getSerializableExtra("IntentExamResultDetail");

其他回答

将对象从一个活动传递到另一个活动。

(1) 源放射性

Intent ii = new Intent(examreport_select.this,
                    BarChartActivity.class);

            ii.putExtra("IntentExamResultDetail",
                    (Serializable) your List<ArraList<String>> object here);
            startActivity(ii);

(2) 目的地活动性

List<ArrayList<String>> aa = (List<ArrayList<String>>) getIntent()
            .getSerializableExtra("IntentExamResultDetail");

如果您选择使用Samuh描述的方式,请记住,只能发送原始值。即,可分割的值。因此,如果您的对象包含复杂对象,这些对象将不会跟随。例如,Bitmap、HashMap等变量。这些变量很难按意图传递。

一般来说,我建议您只将原始数据类型作为额外类型发送,如String、int、boolean等。在您的情况下,它将是:String fname、String lname、int age和String address。

我的观点:通过实现ContentProvider、SDCard等,可以更好地共享更复杂的对象。也可以使用静态变量,但这可能会很快导致容易出错的代码。。。

不过,这只是我的主观看法。

您还可以将对象的数据写入临时字符串和int,并将它们传递给活动。当然,这样可以得到传输的数据,但不能得到对象本身。

但是,如果您只想显示它们,而不在其他方法或类似方法中使用对象,那么就足够了。我以同样的方式在另一个活动中显示一个对象的数据。

String fName_temp   = yourObject.getFname();
String lName_temp   = yourObject.getLname();
String age_temp     = yourObject.getAge();
String address_temp = yourObject.getAddress();

Intent i = new Intent(this, ToClass.class);
i.putExtra("fname", fName_temp);
i.putExtra("lname", lName_temp);
i.putExtra("age", age_temp);
i.putExtra("address", address_temp);

startActivity(i);

你也可以直接传递它们,而不是传递临时变量,但在我看来,这样更清楚。此外,您可以将temp ivars设置为null,以便GarbageCollector更快地清理它们。

祝你好运

附带说明:重写toString()而不是编写自己的打印方法。

正如下面的评论中提到的,这是您在另一个活动中获取数据的方式:

String fName = getIntent().getExtras().getInt("fname");

调用活动时

Intent intent = new Intent(fromClass.this,toClass.class).putExtra("myCustomerObj",customerObj);

在toClass.java中,通过

Customer customerObjInToClass = getIntent().getExtras().getParcelable("myCustomerObj");

请确保客户类实现parcelable

public class Customer implements Parcelable {

    private String firstName, lastName, address;
    int age;

    /* all your getter and setter methods */

    public Customer(Parcel in ) {
        readFromParcel( in );
    }

    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        public LeadData createFromParcel(Parcel in ) {
            return new Customer( in );
        }

        public Customer[] newArray(int size) {
            return new Customer[size];
        }
    };


    @Override
    public void writeToParcel(Parcel dest, int flags) {

        dest.writeString(firstName);
        dest.writeString(lastName);
        dest.writeString(address);
        dest.writeInt(age);
    }

    private void readFromParcel(Parcel in ) {

        firstName = in .readString();
        lastName  = in .readString();
        address   = in .readString();
        age       = in .readInt();
    }

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

A.数据库

B.共享偏好。

C.对象序列化。

可以保存公共数据的类可以命名为公共实用程序。这取决于你。

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

这取决于您的项目需求。

A.数据库

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

教程

B.共享偏好

假设您想存储用户名。所以现在有两件事,一个关键用户名,一个值。

如何存储

 // Create 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()、put Int()、putFloat()和putLong(),可以保存所需的数据类型。

如何获取

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

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

C.对象序列化

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

使用Javabean并将其存储为他的一个字段,并使用getter和setter。

JavaBean是具有财产的Java类。想想作为私有实例变量的财产。因为他们是私人的可以通过类中的方法从类外部访问它们。更改属性值的方法称为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);

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

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

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

如果您需要此教程,请参阅:

Java序列化(博客文章)获取其他类中的变量(堆栈溢出)

D.公用设施

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

样品

public class CommonUtilities {

    public static String className = "CommonUtilities";

}

E.通过意图传递数据

有关传递数据的选项,请参考教程Android–使用Parcelable类在活动之间传递包裹数据。