Problem with a CustomAdapter

0
public class Adaptador_calidades extends SimpleCursorAdapter {
private Context mContext;
private Context appContext;
private int layout;
private Cursor c;
private final LayoutInflater inflater;
int xCajas, xPeso;
double xPrecio,xFinal;

public Adaptador_calidades(Context context, int layout, Cursor c, String[] from, int[] to) {
    super(context, layout, c, from, to);
    this.layout = layout;
    this.mContext = context;
    this.inflater = LayoutInflater.from(context);
    this.c = c;

}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return inflater.inflate(layout, null);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    TextView Cajas = (TextView) view.findViewById(R.id.calCajas);
    TextView Precio = (TextView) view.findViewById(R.id.calPrecio);
    TextView Peso = (TextView) view.findViewById(R.id.calPeso);
    TextView Nombre = (TextView) view.findViewById(R.id.itemCalidad);
    TextView Codigo = (TextView) view.findViewById(R.id.codCalidad);

    Cajas.setText(c.getString(c.getColumnIndexOrThrow("cajas")));
    Precio.setText(c.getString(c.getColumnIndexOrThrow("precio")));
    Peso.setText(c.getString(c.getColumnIndexOrThrow("peso")));
    Nombre.setText(c.getString(c.getColumnIndexOrThrow("nombre")));
    Codigo.setText(c.getString(c.getColumnIndexOrThrow("_id")));

    xCajas = xCajas + Integer.parseInt((c.getString(c.getColumnIndexOrThrow("cajas"))));
    xPeso = xPeso + Integer.parseInt((c.getString(c.getColumnIndexOrThrow("peso"))));
    xPrecio = xPrecio + Double.parseDouble((c.getString(c.getColumnIndexOrThrow("precio"))));

    //Calculamos el peso
    if (!Peso.getText().toString().isEmpty() && !Precio.getText().toString().isEmpty()) {
        xFinal = xFinal + (Double.parseDouble(Peso.getText().toString()) * Double.parseDouble(Precio.getText().toString()));
    }

    Separacion_App Calidades = (Separacion_App)context;
    Calidades.setDatos(xCajas,xPeso,xPrecio,xFinal);
}


public void setDatos(int xCajas, int xPeso, double xPrecio, double xFinal) {
            iCajas.setText(String.valueOf(xCajas));
            iPeso.setText(String.valueOf(xPeso));
            iPrecio.setText(String.valueOf(xPrecio));
            CostoFinal = String.valueOf(xFinal);
    }

My problem is that every time I scroll the ListView it performs the addition again and the variable xFinal throws me a different result every time I slide my ListView.

How can I solve that?

    
asked by DoubleM 22.11.2017 в 21:25
source

1 answer

-1

The sum is obviously done because the bindView() method is called when you slip your ListView .

@Override
public void bindView(View view, Context context, Cursor cursor) {

    ...
    ...

    //Calculamos el peso
    if (!Peso.getText().toString().isEmpty() && !Precio.getText().toString().isEmpty()) {
        xFinal = xFinal + (Double.parseDouble(Peso.getText().toString()) * Double.parseDouble(Precio.getText().toString()));
    }

    ...
    ...

}

If you are correctly storing the Weight and Price data for each item, just get the sum and assign it to xFinal

@Override
public void bindView(View view, Context context, Cursor cursor) {

    ...
    ...

    //Calculamos el peso
    if (!Peso.getText().toString().isEmpty() && !Precio.getText().toString().isEmpty()) {

        //xFinal = xFinal + (Double.parseDouble(Peso.getText().toString()) * Double.parseDouble(Precio.getText().toString()));
        xFinal = (Double.parseDouble(Peso.getText().toString()) * Double.parseDouble(Precio.getText().toString()));

    }

    ...
    ...

}
    
answered by 22.11.2017 в 22:41