How can I make the sum in a list?

0

I need a little help .. I would like to do the sum of the elements of a row in each range of my list ... I am using ListAdapter ... thank you very much ... as in the photo

there's the code

  ListView lista;
ArrayList<HashMap<String, String>> productos;
String url = "http://Servidor/fcm/select.php";
TextView textView ;
int total =0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_compra);

    new Datas().execute();
}
class Datas extends AsyncTask<Void, Void, String> {

    protected String doInBackground(Void... params) {
        lista = (ListView) findViewById(R.id.lista);
        textView=(TextView) findViewById(R.id.tvTotal);
        productos = new ArrayList<>();

        HttpHandler sh = new HttpHandler();
        String jsonStr = sh.makeServiceCall(url);
        Log.d("FINAL", String.valueOf(jsonStr));
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            JSONArray jsonArray = jsonObject.getJSONArray("result");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject c = jsonArray.getJSONObject(i);
                String id = c.getString("id");
                String vende = c.getString("descrip");
                String fecha = c.getString("fecha");
                String detalle = c.getString("detalle");
                HashMap<String, String> contacto = new HashMap<>();
                contacto.put("id", id);
                contacto.put("descrip", vende);
                contacto.put("fecha", fecha);
                contacto.put("detalle", detalle);
                productos.add(contacto);
                Log.d("TIN", String.valueOf(contacto));

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    protected void onPostExecute(String s) {

        super.onPostExecute(s);
        total++;
        final ListAdapter adapter = new SimpleAdapter(Compra.this, productos,
                R.layout.item_pedidos, new String[]{"id", "descrip", "fecha", "detalle"},
                new int[]{R.id.textView2, R.id.textView3, R.id.textView4, R.id.textView5});
        lista.setAdapter(adapter);

         total= lista.getAdapter().getCount();
       Log.d("TOTAL", String.valueOf(total));// Aqui solo llego a contar el nombre de elementos que esta en la lista...
        String res = ""+total;
        textView.setText(res);
      registerForContextMenu(lista);

    }
}
    
asked by Wid Maer 25.08.2017 в 15:18
source

3 answers

0

Ok let's see, Assuming that products, is an arrangement that contains all the information shown in the list, and that id is the value that keeps the number of elements for each product (I'm assuming, your code is not very clear) would be something like that.

  
protected void onPostExecute(String s) {

    super.onPostExecute(s);
    total++;
    final ListAdapter adapter = new SimpleAdapter(Compra.this, productos,
            R.layout.item_pedidos, new String[]{"id", "descrip", "fecha", "detalle"},
            new int[]{R.id.textView2, R.id.textView3, R.id.textView4, R.id.textView5});
    lista.setAdapter(adapter);

   in total = 0;
     for (Producto element : productos){
        total = total + element.getId(); //pon el valor que guarda la cantidad de unidades por producto aqui.
     }
   Log.d("TOTAL", String.valueOf(total));// Aqui solo llego a contar el nombre de elementos que esta en la lista...
    String res = ""+total;
    textView.setText(res);
  registerForContextMenu(lista);

}
  
    
answered by 25.08.2017 в 16:12
0

My proposal is as follows; As you are already traversing the Json when obtaining the elements, take advantage of this cycle to convert the element id to an integer type int and add it. For later in your onPostExecute already have the total amount of the sum of all the rows.

ListView lista;
ArrayList<HashMap<String, String>> productos;
String url = "http://Servidor/fcm/select.php";
TextView textView ;
int total =0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_compra);

    new Datas().execute();
}
class Datas extends AsyncTask<Void, Void, String> {

    protected String doInBackground(Void... params) {
        lista = (ListView) findViewById(R.id.lista);
        textView=(TextView) findViewById(R.id.tvTotal);
        productos = new ArrayList<>();

        total = 0;
        HttpHandler sh = new HttpHandler();
        String jsonStr = sh.makeServiceCall(url);
        Log.d("FINAL", String.valueOf(jsonStr));
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            JSONArray jsonArray = jsonObject.getJSONArray("result");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject c = jsonArray.getJSONObject(i);
                String id = c.getString("id");
                String vende = c.getString("descrip");
                String fecha = c.getString("fecha");
                String detalle = c.getString("detalle");
                HashMap<String, String> contacto = new HashMap<>();
                contacto.put("id", id);
                contacto.put("descrip", vende);
                contacto.put("fecha", fecha);
                contacto.put("detalle", detalle);
                productos.add(contacto);
                Log.d("TIN", String.valueOf(contacto));
                //vas sumando los elementos
                total = total + Integer.parseInt(id);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    protected void onPostExecute(String s) {

        super.onPostExecute(s);
        final ListAdapter adapter = new SimpleAdapter(Compra.this, productos,
            R.layout.item_pedidos, new String[]{"id", "descrip", "fecha", "detalle"},
            new int[]{R.id.textView2, R.id.textView3, R.id.textView4, R.id.textView5});
        lista.setAdapter(adapter);

       Log.d("TOTAL", String.valueOf(total));// Aqui solo llego a contar el nombre de elementos que esta en la lista...
        String res = ""+total;
        textView.setText(res);
      registerForContextMenu(lista);

    }
}
    
answered by 25.08.2017 в 21:42
0

I think the best solution would be to use the arguments of the asynchronous task, I'll leave the modified code:

ListView lista;
ArrayList<HashMap<String, String>> productos;
String url = "http://Servidor/fcm/select.php";
TextView textView ;
int total =0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_compra);

    new Datas().execute();
}
class Datas extends AsyncTask<Void, Void, String> {

    protected String doInBackground(Void... params) {
        lista = (ListView) findViewById(R.id.lista);
        textView=(TextView) findViewById(R.id.tvTotal);
        productos = new ArrayList<>();
        String total;

        HttpHandler sh = new HttpHandler();
        String jsonStr = sh.makeServiceCall(url);
        Log.d("FINAL", String.valueOf(jsonStr));
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            JSONArray jsonArray = jsonObject.getJSONArray("result");
            int sumTotal = 0;
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject c = jsonArray.getJSONObject(i);
                sumTotal += Integer.parseInt(c.getString("id"));  //Aquí haces la sumatoria de la columna
                String id = c.getString("id");
                String vende = c.getString("descrip");
                String fecha = c.getString("fecha");
                String detalle = c.getString("detalle");
                HashMap<String, String> contacto = new HashMap<>();
                contacto.put("id", id);
                contacto.put("descrip", vende);
                contacto.put("fecha", fecha);
                contacto.put("detalle", detalle);
                productos.add(contacto);
                Log.d("TIN", String.valueOf(contacto));

            }
            total = String.valueOf(sumTotal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return total; // regresas la sumatoria en string para que en onPostExecute lo puedas leer
    }

    protected void onPostExecute(String s) {

        super.onPostExecute(s);
        total++;
        final ListAdapter adapter = new SimpleAdapter(Compra.this, productos,
                R.layout.item_pedidos, new String[]{"id", "descrip", "fecha", "detalle"},
                new int[]{R.id.textView2, R.id.textView3, R.id.textView4, R.id.textView5});
        lista.setAdapter(adapter);

         total= lista.getAdapter().getCount();
        String res = ""+total;
        textView.setText(s);
      registerForContextMenu(lista);

    }
}

You would do the summation within doInBackground() and then return in the same method the total of the column, in the end you would only have to take the parameter of onPostExecute() and set it in the textView .

    
answered by 25.08.2017 в 22:35