Store data in list and make accounts with them without database

0

I have a Recyclerview that shows a list of foods, each row of the recyclerview has 2 buttons, one for quantity of the food and the other to decrease the quantity of the food, graphically there is something like this:

Coca-Cola Price: 10.00 - 0 + Fanta Price: 8.00 - 0 +

That would be the Recyclerview I want that each person of the button to keep the food, the amount they have currently put and their price.

Then pass it to collect showing a receipt.

How to save in real time each time the person gives + a meal the food, its quantity and its price.

So far I have this

holder.botonMasComida.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

                holder.tvCantidadComida.setText("" + (cantidadComida = cantidadComida + 1));
            RestaurantDetalle.agregarComida(mealList.get(position).getName());
        }
    });

Activity

static public void agregarComida(String comida){

    listaComida.add(comida);

}
    
asked by Eduardo Ricardez 21.04.2017 в 15:48
source

2 answers

1

Maybe you should do a Array of for example 100 positions where the coca-tails or fantasy will be saved, and then you can perform a FOR along with a > IF and a variable that works as an accountant to find in each position how many coca-tails there are and also stops the fantas:).

    
answered by 21.04.2017 в 16:02
0

I solved it like this:

static public void agregarComida(String comida){

    listaComida.add(comida);
    hacerCuentasComida();

}

private static void hacerCuentasComida(){

    Map<String, Integer> contador = new HashMap<>();
    Map<String, Double> precio = new HashMap<>();
    List<String> duplicateList = new ArrayList<>();

    for (String item : listaComida) {

        String[] parts = item.split(",");

        if (!contador.containsKey(parts[0])) {

            duplicateList.add(parts[0]);
            contador.put(parts[0], 1);

            //Sacar precio
            precio.put(parts[0], Double.valueOf(parts[1]));
        } else {
            Integer count = contador.get(parts[0]);
            contador.put(parts[0], count + 1);
            count = contador.get(parts[0]);
            precio.put(parts[0], (count*Double.valueOf(parts[1])));
        }
    }


    Log.d("prueba1", String.valueOf(duplicateList));
    Log.d("prueba2", String.valueOf(contador));
    Log.d("prueba3", String.valueOf(precio));
    Log.d("textoCompleto","x"+contador.get(duplicateList.get(0))+" "+duplicateList.get(0)+" "+precio.get(duplicateList.get(0))+"€");
}

Adapter:

holder.botonMasComida.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                mealList.get(position).setCantidadComida(mealList.get(position).getCantidadComida()+1);
                holder.tvCantidadComida.setText("" + mealList.get(position).getCantidadComida());
            RestaurantDetalle.agregarComida(mealList.get(position).getName()+","+mealList.get(position).getPrice());
        }
    });
    
answered by 22.04.2017 в 01:09