gpt4 book ai didi

java - 如何使具有多个整数和字符串的类可解析?

转载 作者:行者123 更新时间:2023-12-01 21:45:17 25 4
gpt4 key购买 nike

我有以下Class,我想让它Parcelable问题是,当我通过方法 in.createIntArray()int[] arraySongIntegers 读取 isHidden 和 Counter 变量时,我得到一个空数组大小为 0 且顺便说一句,所有字符串变量都被正确读取。请告诉我如何将所有变量写入 Parcel 并正确读回。

public class Song implements Parcelable{
public static long firstSongId;
private long id;
private String title ,mimeType, artist , dateAdded ,album;
private int isHidden ,counter; // 0 is false , 1 is true

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

@Override
public void writeToParcel(Parcel dest, int flags) {
int[] arraySongIntegers = new int[2];
arraySongIntegers[0] = isHidden;
arraySongIntegers[1] = counter;
dest.writeIntArray(arraySongIntegers);


dest.writeLong(id);

List<String> l = new ArrayList<>();
l.add(title);
l.add(artist);
l.add(album);
l.add(dateAdded);
dest.writeStringList(l);

}


public void readFromParcel(Parcel in){

id = in.readLong();

int[] arraySongIntegers = in.createIntArray();
isHidden = arraySongIntegers[0];
counter = arraySongIntegers[1];

ArrayList<String> list = in.createStringArrayList();
title = list.get(0);
artist = list.get(1);
album = list.get(2);
dateAdded = list.get(3);
}

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

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

}

提前致谢。

最佳答案

除了我的评论(没有理由将其设为数组)之外,您还需要按照编写的顺序读取所有内容。首先读取 id 一切都会搞砸。

关于java - 如何使具有多个整数和字符串的类可解析?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36071023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com