安卓:制作类parcelableparcelable

2023-09-05 09:32:43 作者:素颜☆如水

我想通过在Android的两项活动的对象,它有使我parcelable类。我不是想转换我当前的类,但不了解Parcelable.Creator方法。

这是我迄今为止

 公共类账户实现Parcelable {

/ **
 *声明私有变量
 * /

私人字符串FNAME;
私人字符串LNAME;
私人字符串屏幕名;
私人字符串电子邮件;
私人字符串的水平;
私人诠释用户id;
//私人语境myContext;

/ **
 *帐号构造
 * /

公共帐户(字符串FNAME,LNAME字符串,字符串SNAME,字符串email,字符串的水平,字符串x)抛出异常{


    / **解析用户id转换成int * /
    this.userId =的Integer.parseInt(X);

    / **从数据库用户的详细信息,选择* /


    / **与结果初始化变量* /
    this.setfName(FNAME);
    this.setlName(LNAME);
    this.setScreenName(SNAME);
    this.setEmail(电子邮件);
    this.setLevel(水平);



}

@覆盖
公众诠释describeContents(){
    // TODO自动生成方法存根
    返回0;
}

@覆盖
公共无效writeToParcel(包裹出来,诠释标志){
    // TODO自动生成方法存根
    out.writeString(FNAME);
    out.writeString(LNAME);
    out.writeString(屏幕名);
    out.writeString(电子邮件);
    out.writeString(水平);
    out.writeInt(用户id);

}

//这是用来生成你的对象。所有parcelables的必须有一个实现这两种方法CREATOR
公共静态最终Parcelable.Creator<帐户> CREATOR =新Parcelable.Creator<帐户>(){
    公共账号createFromParcel(包裹中){
        返回新帐户(在);
    }

    公共科目[] newArray(INT尺寸){
        返回新帐号【尺寸】;
    }
};



/ **
 * getter和setter从Account类的所有变量
 * /

公众诠释getUserId(){
    返回用户id;
}

公共字符串getScreenName(){
    返回屏幕名;
}

公共无效setScreenName(字符串屏幕名){
    this.screenName =屏幕名;
}

公共字符串getfName(){
    返回FNAME;
}

公共无效setfName(字符串FNAME){
    this.fName = FNAME;
}

公共字符串getEmail(){
    返回的电子邮件;
}

公共无效setEmail(字符串电子邮件){
    this.email =电子邮件;
}

公共字符串getLevel(){
    回报水平;
}

公共无效执行setLevel(串级){
    this.level =水平;
}

公共字符串getlName(){
    返回LNAME;
}

公共无效setlName(字符串LNAME){
    this.lName = LNAME;
}



}
 

在此先感谢。

解决方案

 公共静态最终Parcelable.Creator CREATOR =
    新Parcelable.Creator(){
        公共对象B createFromParcel(包裹中){
            返回新对象B(中);
        }

        公共对象B [] newArray(INT尺寸){
            返回新对象B【尺寸】;
        }
    };
 
EMUI10公测版来袭 mate20也可以更新了,这三项功能已经被修复了

本场需要为Android,以便能够创建新的对象,单独或数组。这也意味着,你可以使用使用默认的构造函数来创建对象,并使用另一种方法,需要hyrdate吧。

看这机器人 - Parcelable

创建Parcelable类

在线工具

和 http://www.appance.com/tag/parcelable/

I want to pass on object between two activities in Android which has lead me to parcelable classes. I am not trying to convert my current class but don't understand the Parcelable.Creator method.

This is what I have so far

public class Account implements Parcelable{

/**
 * Declare private variables
 */

private String fName;
private String lName;
private String screenName;
private String email;
private String level;
private int userId;
//private Context myContext;

/**
 * Account constructor
 */

public Account(String fName, String lName, String sName, String email, String level, String x) throws Exception{    


    /**Parse userId into int */
    this.userId = Integer.parseInt(x);

    /**Select from DB user details */


    /**Initialize variables with results */
    this.setfName(fName);
    this.setlName(lName);
    this.setScreenName(sName);
    this.setEmail(email);
    this.setLevel(level);



}

@Override
public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public void writeToParcel(Parcel out, int flags) {
    // TODO Auto-generated method stub
    out.writeString(fName);
    out.writeString(lName);
    out.writeString(screenName);
    out.writeString(email);
    out.writeString(level);
    out.writeInt(userId);

}

// this is used to regenerate your object. All Parcelables must have a CREATOR that implements these two methods
public static final Parcelable.Creator<Account> CREATOR = new Parcelable.Creator<Account>() {
    public Account createFromParcel(Parcel in) {
        return new Account(in);
    }

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



/**
 * Getters and setters for all variables from Account class
 */

public int getUserId(){
    return userId;
}

public String getScreenName() {
    return screenName;
}

public void setScreenName(String screenName) {
    this.screenName = screenName;
}

public String getfName() {
    return fName;
}

public void setfName(String fName) {
    this.fName = fName;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getLevel() {
    return level;
}

public void setLevel(String level) {
    this.level = level;
}

public String getlName() {
    return lName;
}

public void setlName(String lName) {
    this.lName = lName;
}



}

Thanks in advance.

解决方案

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

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

This field is needed for Android to be able to create new objects, individually or as arrays. This also means that you can use use the default constructor to create the object and use another method to hyrdate it as necessary.

Look at this Android – Parcelable

Online tool for creating Parcelable class

and http://www.appance.com/tag/parcelable/

 
精彩推荐
图片推荐