Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android Parcelable类中读取和写入长对象_Android_Object_Long Integer_Parcelable - Fatal编程技术网

在Android Parcelable类中读取和写入长对象

在Android Parcelable类中读取和写入长对象,android,object,long-integer,parcelable,Android,Object,Long Integer,Parcelable,如果说我有一个长长的目标。下面的例子是否演示了正确的读写长对象的方法 Class MyClass implements Parcelable { private Long aLongObject; public static final Creator<MyClass> CREATOR = new Creator<MyClass>() { @Override public MyClass createFromParcel(Parcel in

如果说我有一个长长的目标。下面的例子是否演示了正确的读写长对象的方法

Class MyClass implements Parcelable {
   private Long aLongObject;

    public static final Creator<MyClass> CREATOR = new Creator<MyClass>() {
    @Override
    public MyClass createFromParcel(Parcel in) {
        return new MyClass(in);
    }

    @Override
    public MyClass[] newArray(int size) {
       .....
    }
};


protected MyClass(Parcel in) {// reading Parcel
    super(in);

  aLongObject = in.readLong(); // correct way to ready Long object?
 }

   @Override
public void writeToParcel(@NonNull Parcel dest, int flags) {
    super.writeToParcel(dest, flags);

      dest.writeLong(aLongObject); // is this correct way to send Long?
  }
}
Class MyClass实现可包裹{
私有长期对象;
公共静态最终创建者=新创建者(){
@凌驾
公共MyClass createFromParcel(地块中){
返回新的MyClass(在中);
}
@凌驾
公共MyClass[]新数组(整数大小){
.....
}
};
受保护的MyClass(包裹中){//正在读取包裹
超级(in),;
aLongObject=in.readLong();//准备长对象的正确方法?
}
@凌驾
公共无效writeToParcel(@NonNull Parcel dest,int标志){
超级可写包裹(目的地、旗帜);
dest.writeLong(aLongObject);//这是发送Long的正确方法吗?
}
}

您的方法的问题是
Long
值可能是
null
,但是
Parcel
方法仅获取/返回原始数据类型
Long
的值,有关详细信息,请参阅。因此,您需要一种变通方法来存储
Long

我喜欢使用
int
来指示我的
Long
值是否为
null
(只能存储
布尔值
数组,没有单个
布尔值),如果不是,则使用
Long
来存储数值:

写入
包裹

int indicator = (aLongObject == null) ? 0 : 1;
long number = (aLongObject == null) ? 0 : aLongObject;
dest.writeInt(indicator); 
dest.writeLong(number); 
// NOTE: reading must be in the same order as writing
Long aLongObject;
int indicator = in.readInt();
long number = in.readLong();
if(indicator == 0){
    aLongObject = null;
}
else{
    aLongObject = number;
}
包裹读取数据

int indicator = (aLongObject == null) ? 0 : 1;
long number = (aLongObject == null) ? 0 : aLongObject;
dest.writeInt(indicator); 
dest.writeLong(number); 
// NOTE: reading must be in the same order as writing
Long aLongObject;
int indicator = in.readInt();
long number = in.readLong();
if(indicator == 0){
    aLongObject = null;
}
else{
    aLongObject = number;
}
你可以简单地使用

写 阅读
writeValue
readValue
优雅地处理
null


请参阅

谢谢您的回答。快速提问,既然这是一个对象,那么使用dest.writeValue和in.readValue是最好的方法吗?@Akshay-我不明白的是,为什么在内部有一个
类MyClass implements Parcelable
和一个
V3MemberReward
的构造函数。有一个很好的教程,可能会有帮助。对不起,输入错误。。我已经修好了that@Akshay-我认为使用两个基本数据类型值来映射
Long
值比使用
对象
要好,因为
对象
创建成本更高(内存和CPU时间)。奥托,我怀疑这对现代设备会有多大影响