【问题标题】:Android: How do I work with IntArrays in Parcelable?Android:如何在 Parcelable 中使用 IntArrays?
【发布时间】:2018-04-25 20:44:16
【问题描述】:

在我的应用程序中,我有一个类CurrentGame,我根据这个问题中的示例设置了它:Android Class Parcelable with ArrayList

但是,我不知道如何将 intArray scoresRnd 添加到 Parcel 构造函数中。

例如,当我添加这一行时,它会以红色下划线显示:

this.scoresRnd = parcel.readArray(null);

...我收到此错误消息“不兼容的类型。必需:Int[]。找到:java.lang.Object[]”。你能帮我么?谢谢! (另外,请指出我的代码中的任何其他错误)

public class CurrentGame implements Parcelable{
    private int rnd;
    private int[] scoresRnd;
    private ArrayList<int[]> scoreList;

    public CurrentGame(){
        super();
        this.rnd = 0;
        this.scoresRnd = new int[]{0,0,0,0};
        this.scoreList = new ArrayList<>();
    }

    public CurrentGame (Parcel parcel){
        this.scoreList = parcel.readArrayList(null);
        this.rnd = parcel.readInt();
        //WHAT DO I PUT HERE FOR THE INTARRAY???

    }

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

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        // TODO Auto-generated method stub
        parcel.writeList(this.scoreList);
        parcel.writeInt(this.rnd);
    }

    public static final Creator<CurrentGame> CREATOR = new Creator<CurrentGame>() {

        @Override
        public CurrentGame createFromParcel(Parcel parcel) {
            // TODO Auto-generated method stub
            return new CurrentGame(parcel);
        }

        @Override
        public CurrentGame[] newArray(int i) {
            // TODO Auto-generated method stub
            return new CurrentGame[i];
        }
    };

    public void expandList(int[] scoresRnd){
        scoreList.add(scoresRnd);
    }

    public void setRound(int rnd){
        this.rnd=rnd;
    }

    public int getRound(){
        return rnd;
    }

    public ArrayList<int[]> getScoreList(){
        return scoreList;
    }
}

【问题讨论】:

    标签: java android parcelable


    【解决方案1】:

    试试这个

    @Override
    public int describeContents() {
        return 0;
    }
    
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.rnd);
        dest.writeIntArray(this.scoresRnd);
        dest.writeList(this.scoreList);
    }
    
    public s() {
    }
    
    protected s(Parcel in) {
        this.rnd = in.readInt();
        this.scoresRnd = in.createIntArray();
        this.scoreList = new ArrayList<int[]>();
        in.readList(this.scoreList, int[].class.getClassLoader());
    }
    
    public static final Parcelable.Creator<s> CREATOR = new Parcelable.Creator<s>() {
        @Override
        public s createFromParcel(Parcel source) {
            return new s(source);
        }
    
        @Override
        public s[] newArray(int size) {
            return new s[size];
        }
    };
    

    【讨论】:

    • 谢谢,我试试!但是还有一种方法不会过多地改变我现有的代码吗?还是我的代码方法不对?
    • dest.writeIntArray(this.scoresRnd); 是您所需要的。如果你需要它来自动生成,Android Studio 中有很多插件。所以你不用担心Parcelable
    • 好的,所以我输入了 'dest.writeIntArray(this.scoresRnd);'进入“writeToParcel”方法。但是我应该在我的 Parcel 构造函数中添加什么?
    • 那里什么都不需要。让它保持原样。或者让它为空。这是你的选择。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 2011-10-10
    • 2011-10-25
    • 1970-01-01
    • 2018-10-14
    • 1970-01-01
    相关资源
    最近更新 更多