我正在尝试将具有Double参数的某个对象的Arralyist从一个活动传递到另一个活动,但是在发送它之后,Double的结果是不同的。我的对象产品实现了可拆分]
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;
}
}
我正在尝试将其发送到产品数组列表中的下一个活动。首次活动
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
但是当我在新活动中收到arraylist时,值不相同
第二活动
ArrayList<Producto> listadoProductos = new ArrayList<>()
Intent intent = getIntent();
if (intent.getParcelableArrayListExtra("nombres")!= null) {
listadoProductos = intent.getParcelableArrayListExtra("nombres");
此处,新值为9.458744551493758E-13任何人都可以解释这是怎么回事,以及如何获得8.92的实际价值?
使用包裹包裹时,您必须具有正确的顺序。
阅读字段时,您必须与编写它们时具有相同的顺序。您在这里:
// 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();
它应该起作用