Amount and not repeated in ArrayList Android

2

Hi, I have an arraylist with the object prePedido . which is composed with its ID, an IDcliente, an IDproduct and an idRestaurant.

What I need is to build another array without repeating the product ID. And if it is repeated just keep it 1 time and count how many times it repeats. That is, if my array has 2 identical products, with the same IDproduct, in an array save only 1 time and count how many times it is repeated is to say if it is repeated 1 time it would show a 2. Here my code in an Onresponse going through it to pass it to an adapter.

 public void onResponse(JSONObject response) {

    JSONArray json = response.optJSONArray("prePedido");
    JSONObject jsonObject = null;
    try {
        int i = 0;
        for(i=0; i<json.length();i++){
            prePedido PrePedido = new prePedido();
            jsonObject = json.getJSONObject(i);
            PrePedido.setNombre_producto(jsonObject.optString("nombre"));
            PrePedido.setPrecio(jsonObject.optDouble("precio"));
            PrePedido.setIdCliente(jsonObject.optInt("idCliente"));
            PrePedido.setIdProducto(jsonObject.optInt("idProducto"));
            PrePedido.setIdRestaurant(jsonObject.optInt("idRestaurant"));
            PrePedido.setId(jsonObject.optInt("idPrePedido"));
            precio = jsonObject.optDouble("precio")+precio;
            pedidos.add(PrePedido);
        }
        adapter = new PrePedidoAdapter(this, pedidos, usuario);
        lista.setAdapter(adapter);
        total.setText("El total es: $"+String.valueOf(precio)+'0');

    } catch (JSONException e) {
        e.printStackTrace();
    }
}
    
asked by Juampi 12.12.2018 в 20:36
source

1 answer

2

To do this:

  

What I need is to build another array without repeating the product ID.

first declares another ArrayList where the non-repeated elements will be stored:

List<prePedido> pedidosNoRepetidos = new ArrayList<>();

and you can insert elements to this array ensuring that the element does not exist if it exists determines not to add it, this can be added after adding the element to the ArrayList pedidos :

   ...
   ...
                pedidos.add(PrePedido);

                boolean repetido = false;
                for(int j=0; j<pedidosNoRepetidos.size();j++){
                    if(pedidosNoRepetidos.get(j).getIdproducto() == PrePedido.getIdproducto()){
                        //Se encuentra actualmente en ArrayList el elemento.
                        repetido = true;
                        break; //Si encuentra un elemento repetido deja de buscar en el ArrayList.
                    }
                }
                if(!repetido){ //Agrega si el elemento no se encuentra repetido en el ArrayList
                    pedidosNoRepetidos.add(PrePedido); //Agrega en Arraylist de elementos no repetidos
                }

            }

To indicate repeated elements you can add the prePedido % property to your cantidad :

   private int cantidad = 0;

    public int getCantidad() {
        return cantidad;
    }

    public void setCantidad(int cantidad) {
        this.cantidad = cantidad;
    }

Now when you find that the item is already contained, add 1 to the property:

        ...
        ...
        for(int j=0; j<pedidosNoRepetidos.size();j++){
            if(pedidosNoRepetidos.get(j).getIdproducto() == PrePedido.getIdproducto()){
                //Se encuentra actualmente en ArrayList el elemento.


                pedidos.get(j).setCantidad(pedidos.get(j).getCantidad() + 1);

                repetido = true;
                break; //Si encuentra un elemento repetido deja de buscar en el ArrayList.
            }
        }
        ...
        ...

You can use the getCity () property to get the number of times the item is repeated.

When you finish the for you can use this other for to print the information:

    for(prePedido p : pedidos){
        Log.i(TAG, "Elemento: " + p.getIdproducto() +" repetido: " + p.getCantidad() + " veces.");
    }
    
answered by 13.12.2018 / 01:31
source