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