Double 的奇怪结果从 activity 到 activity
Strange result on Double from activity to activity
我试图将某个带有 Double 参数的对象的 Arralyist 从一个 activity 传递给另一个 activity,但是在发送它之后,Double 结果不一样。
我的对象 Producto 实现了 parcelable
import android.os.Parcel;
import android.os.Parcelable;
public class Producto implements Parcelable {
private String nombre, descripcion, url, tipo;
private Double precio;
private int cantidad;
public Producto(String nombre, String descripcion, Double precio, String url, String tipo){
this.nombre = nombre;
this.descripcion = descripcion;
this.precio = precio;
this.tipo = tipo;
this.url = url;
}
protected Producto(Parcel in){
nombre = in.readString();
descripcion = in.readString();
url = in.readString();
tipo = in.readString();
precio = in.readDouble();
cantidad = in.readInt();
}
public static final Creator<Producto> CREATOR = new Creator<Producto>() {
@Override
public Producto createFromParcel(Parcel source) {
return new Producto(source);
}
@Override
public Producto[] newArray(int size) {
return new Producto[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(nombre);
dest.writeString(descripcion);
dest.writeString(url);
dest.writeString(tipo);
dest.writeInt(cantidad);
dest.writeDouble(precio);
}
public static Creator<Producto> getCreator(){
return CREATOR;
}
}
我正在尝试将其发送到产品数组列表中的下一个 activity。
首先Activity
for (DocumentSnapshot doc: listadoProductos
) {
p = new Producto(doc.getString("Nombre"), doc.getString("Descripcion"),
doc.getDouble("Precio"), doc.getString("url2"), doc.getString("Tipo"));
nombres.add(p);
}
Intent intent = new Intent(getApplicationContext(), Productos.class);
intent.putParcelableArrayListExtra("nombres",nombres);
startActivity(intent);
而且我已经检查过,此时 Precio 的值没问题,在我的情况下是 8.92
但是当我在新的 Activity 中收到数组列表时,值不一样
第二个Activity
ArrayList<Producto> listadoProductos = new ArrayList<>()
Intent intent = getIntent();
if (intent.getParcelableArrayListExtra("nombres")!= null) {
listadoProductos = intent.getParcelableArrayListExtra("nombres");
此处,新值为9.458744551493758E-13
任何人都可以解释发生了什么以及如何获得 8.92 的实际值?
使用 parcelable 时,您必须准备好正确的订单。
阅读您的字段时,您的顺序必须与您编写它们时的顺序相同。这里有:
// writing: first cantidad then precio
dest.writeInt(cantidad);
dest.writeDouble(precio);
// reading is reversed.
precio = in.readDouble();
cantidad = in.readInt();
只需更改顺序即可
cantidad = in.readInt();
precio = in.readDouble();
它应该可以工作
我试图将某个带有 Double 参数的对象的 Arralyist 从一个 activity 传递给另一个 activity,但是在发送它之后,Double 结果不一样。 我的对象 Producto 实现了 parcelable
import android.os.Parcel;
import android.os.Parcelable;
public class Producto implements Parcelable {
private String nombre, descripcion, url, tipo;
private Double precio;
private int cantidad;
public Producto(String nombre, String descripcion, Double precio, String url, String tipo){
this.nombre = nombre;
this.descripcion = descripcion;
this.precio = precio;
this.tipo = tipo;
this.url = url;
}
protected Producto(Parcel in){
nombre = in.readString();
descripcion = in.readString();
url = in.readString();
tipo = in.readString();
precio = in.readDouble();
cantidad = in.readInt();
}
public static final Creator<Producto> CREATOR = new Creator<Producto>() {
@Override
public Producto createFromParcel(Parcel source) {
return new Producto(source);
}
@Override
public Producto[] newArray(int size) {
return new Producto[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(nombre);
dest.writeString(descripcion);
dest.writeString(url);
dest.writeString(tipo);
dest.writeInt(cantidad);
dest.writeDouble(precio);
}
public static Creator<Producto> getCreator(){
return CREATOR;
}
}
我正在尝试将其发送到产品数组列表中的下一个 activity。 首先Activity
for (DocumentSnapshot doc: listadoProductos
) {
p = new Producto(doc.getString("Nombre"), doc.getString("Descripcion"),
doc.getDouble("Precio"), doc.getString("url2"), doc.getString("Tipo"));
nombres.add(p);
}
Intent intent = new Intent(getApplicationContext(), Productos.class);
intent.putParcelableArrayListExtra("nombres",nombres);
startActivity(intent);
而且我已经检查过,此时 Precio 的值没问题,在我的情况下是 8.92
但是当我在新的 Activity 中收到数组列表时,值不一样
第二个Activity
ArrayList<Producto> listadoProductos = new ArrayList<>()
Intent intent = getIntent();
if (intent.getParcelableArrayListExtra("nombres")!= null) {
listadoProductos = intent.getParcelableArrayListExtra("nombres");
此处,新值为9.458744551493758E-13 任何人都可以解释发生了什么以及如何获得 8.92 的实际值?
使用 parcelable 时,您必须准备好正确的订单。
阅读您的字段时,您的顺序必须与您编写它们时的顺序相同。这里有:
// writing: first cantidad then precio
dest.writeInt(cantidad);
dest.writeDouble(precio);
// reading is reversed.
precio = in.readDouble();
cantidad = in.readInt();
只需更改顺序即可
cantidad = in.readInt();
precio = in.readDouble();
它应该可以工作