【问题标题】:Save 2d array of custom object onSaveInstanceState保存自定义对象的二维数组 onSaveInstanceState
【发布时间】:2014-07-06 21:06:23
【问题描述】:

您好,我正在尝试在 onSaveInstanceState 方法内的 Bundle 中保存自定义对象的二维数组,以便在用户旋转屏幕时保存自定义对象数组 但我不知道如何实现它。如果可能的话,就像一个“简单”的解决方案......谢谢

这是自定义对象的类,我尝试添加实现 Parcelable & 方法但不确定该部分是否正确:

package com.example.game
import android.os.Parcel;
import android.os.Parcelable;
//hacemos el implements parcelable para que se puede guardar en el savedinstance
public class NumeroCarton implements Parcelable  {
    protected int numero;
    protected boolean numacertado;
    public NumeroCarton(int numero) {
        this.numacertado =false;
        this.numero = numero;
    }



    public int getNumero() {
        return numero;
    }
    public void setNumero(int numero) {
        this.numero = numero;
    }
    public boolean isNumacertado() {
        return numacertado;
    }
    public void setNumacertado(boolean numacertado) {
        this.numacertado = numacertado;
    }


    ////parcelable  :

    private NumeroCarton(Parcel in) {
        numero = in.readInt();
    }

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }
    @Override
    public void writeToParcel(Parcel out, int flags) {
        out.writeInt(numero);

    }

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

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

好的,在我想要保存的对象的活动内部,它的 micarton,一个 2d 数组 我上面写的那门课:

NumeroCarton[][] micarton=new NumeroCarton[5][3];

我想将它保存在 onSaveInstanceState 方法的包中:

public void onSaveInstanceState(Bundle outInstance) {
        super.onSaveInstanceState(outInstance);
        outInstance.putInt(STATE_PUNTOS, puntos);


//This is my problem: 
outInstance.putParcelable(STATE_MICARTONN, micarton);

       //The method putParcelable(String, Parcelable)
// in the type Bundle is not  //applicable for the arguments 
//(String, NumeroCarton[][])
//Neither outInstance.putParcelableArray[](STATE_MICARTONN, micarton)

}

【问题讨论】:

    标签: android arrays bundle parcelable


    【解决方案1】:

    我认为,如果您使自定义类可序列化,它将起作用。

    【讨论】:

    • 嗯尝试将此添加到自定义类中,但没有成功。我添加了实现 Serializable 和 private static final long serialVersionUID
    • public class NumeroCarton 实现 Parcelable ,Serializable { private static final long serialVersionUID = -3644984878389115644L;
    • outInstance.putSerializable(STATE_MICARTONN, micarton);micarton=(NumeroCarton[][]) savedInstanceState.getSerializable(STATE_MICARTONN); 在添加序列化后做了谢谢!!
    【解决方案2】:

    还有另一个简单的解决方案,尝试将第二个数组转换为 csv 字符串,如下所示:

    data11,data12,data13
    data21,data22,data23
    ....
    

    在这种格式下,您可以轻松地将第二个数组转换为字符串并向后转换

    【讨论】:

    • 谢谢,很有趣,但我必须遍历数组并在每个 [][].getInt 上写为字符串,对于 [][] 布尔值,如果为真,则写一个 if 语句字符串,如果我理解正确,则为 false,您的意思是将 2d 数组转换为字符串
    • 这就是我的意思,但是如果不需要语句,当您将其附加到字符串缓冲区时,将自动添加 ture 或 false。注意,这是一种简单但不高效的方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-15
    • 1970-01-01
    • 1970-01-01
    • 2019-05-27
    • 1970-01-01
    • 2017-09-06
    • 1970-01-01
    相关资源
    最近更新 更多