Custom adapter adds rows when scrolling

3

I have created my custom adapter class, which is responsible for loading the custom elements within my listview.

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);

   //Datos desde sqlite, los mostramos en el listview personalizado
    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; //Mando la suma al textview total
         Calidades.setDatos(xCajas, xPeso, xPrecio, xFinal);
}

Constructor that receives the information and shows it in the total:

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);
}
  

Send my adapter

public void ConsultarCalidad() {
    try {
        Cursor cursor = basededatos.ConsultarCalidad();
        if (cursor == null) {
            Toast.makeText(this, "Se ha presentado un problema al cargar", Toast.LENGTH_LONG).show();
            return;
        }

        if (cursor.getCount() == 0) { 
            Toast.makeText(this, "Ningún elemento", Toast.LENGTH_LONG).show();
        }

        String[] columns = new String[]{
                basededatos.COLUMN_NOMBRE,
                basededatos.COLUMN_IDCALIDAD,
                basededatos.COLUMN_CAJAS,
                basededatos.COLUMN_PESO,
                basededatos.COLUMN_PRECIO
        };

        int[] boundTo = new int[]{
                R.id.itemCalidad,
                R.id.codCalidad,
                R.id.calCajas,
                R.id.calPeso,
                R.id.calPrecio
        };

//Mando llamar a mi customadapter anter mencionado.
        customAdapter = new Adaptador_calidades(this, R.layout.item_calidad, cursor, columns, boundTo);
        ListCalidades.setAdapter(customAdapter);

    } catch (Exception ex) {
        Toast.makeText(this, "Se ha producido un error "+ex, Toast.LENGTH_LONG).show();
    }

I think it's the most important thing to mention. Apparently everything is in the bindView or some other method that I can use? When I give a save of dialog I will call the ConsultarCalidad() method again

    
asked by DoubleM 18.02.2018 в 03:20
source

1 answer

1

In my opinion, you should not make such calculations in the adapter , the responsibility of adapter should be adapt the data to the display elements that you have decided to use.

Having said that, instead of keeping the totals in the adapter, you would simply scroll the cursor to calculate the totals:

public void ConsultarCalidad() {
    try {
        Cursor cursor = basededatos.ConsultarCalidad();
        if (cursor == null) {
            Toast.makeText(this, "Se ha presentado un problema al cargar", Toast.LENGTH_LONG).show();
            return;
        }

        if (cursor.getCount() == 0) { 
            Toast.makeText(this, "Ningún elemento", Toast.LENGTH_LONG).show();
        } else {
            //calcular totales

            int xCajas, xPeso;
            double xPrecio,xFinal;

            do {
                int peso = Integer.parseInt(cursor.getString(cursor.getColumnIndexOrThrow("peso")));
                double precio = Double.parseDouble(cursor.getString(cursor.getColumnIndexOrThrow("precio")));
                xPeso += peso;
                xPrecio += precio;
                xCajas += Integer.parseInt(cursor.getString(cursor.getColumnIndexOrThrow("cajas")));

                xFinal += peso * precio;

            } while (cursor.moveToNext());

            cursor.moveToFirst();

            setDatos(xCajas, xPeso, xPrecio, xFinal);
        }

        String[] columns = new String[]{
                basededatos.COLUMN_NOMBRE,
                basededatos.COLUMN_IDCALIDAD,
                basededatos.COLUMN_CAJAS,
                basededatos.COLUMN_PESO,
                basededatos.COLUMN_PRECIO
        };

        int[] boundTo = new int[]{
                R.id.itemCalidad,
                R.id.codCalidad,
                R.id.calCajas,
                R.id.calPeso,
                R.id.calPrecio
        };

        //Mando llamar a mi customadapter anter mencionado.
        customAdapter = new Adaptador_calidades(this, R.layout.item_calidad, cursor, columns, boundTo);
        ListCalidades.setAdapter(customAdapter);

    } catch (Exception ex) {
        Toast.makeText(this, "Se ha producido un error "+ex, Toast.LENGTH_LONG).show();
    }
}
    
answered by 02.03.2018 / 16:29
source