0

I have a program which shows the total price and quantity of different products like Kebab wrap and Chicken Burger

Here is the problem. I have a linear layout (or scrollable view) whatever it's called and it displays the quantity and total price, but not for every item.

For example, if I click on Kebab Wrap 3 times, and Chicken Burger 3 times, I will get this on the scrollable view:

quantity = 3
Total price = 26.94

However, what I want is this to appear

3 Kebab Wrap @£4.99 = £14.97;
3 Chicken Burger @3.99 = £11.97;
Total = £26.94

The problem is that all items are sharing the quantity and price with each other, which is wrong. I need them to show their own unique quantity, own unique total price, then the overall total price of everything.

So to make it clear: what i want is for the scrollable view to store my products on every time I click the button item, like Kebab Wrap.

I have tried several things but I keep getting errors. Please help me

The items are in a hashmap. I will show you the code below

package com.example.aa1172.the_improved_almighty_project;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.HashMap;
import java.util.Map;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    static  HashMap<String, Double> menu = new HashMap<String, Double>();
    public TextView tally;

    static final HashMap<String, Integer> list = new HashMap<>();


    public static float round2(float number, int scale) {
        int pow = 10;
        for (int i = 1; i < scale; i++)
            pow *= 10;
        float tmp = number * pow;
        return ( (float) ( (int) ((tmp - (int) tmp) >= 0.5f ? tmp + 1 : tmp) ) ) / pow;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        menu.put("Cheese Burger", 1.99);
        menu.put("Kebab Wrap", 4.99);
        menu.put("Mayo Chicken", 0.99);
        menu.put("Lamb Doner", 3.99);
        menu.put("Biscuits", 1.99);
        menu.put("Ice Cream", 1.49);
        menu.put("Chicken Burger", 3.99);
        menu.put("BBQ Chicken Burger", 3.49);
        menu.put("Zinger Burger", 2.99);


        Button varKebab = (Button) findViewById(R.id.Kebab);
        Button varIceCream = (Button) findViewById(R.id.IceCream);
        Button varBiscuits = (Button) findViewById(R.id.Biscuits);
        Button varLambDoner = (Button) findViewById(R.id.LambDoner);
        Button varChickenBurger = (Button) findViewById(R.id.ChickenBurger);
        final TextView varTotal = (TextView) findViewById(R.id.Total);
        final TextView varTotalPrice = (TextView) findViewById(R.id.TotalPrice);
        final LinearLayout varItems = (LinearLayout) findViewById(R.id.Items);


        final HashMap<String, Integer> list = new HashMap<>();

        varKebab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (menu.containsKey("Kebab Wrap")) {
                    Integer quantity = list.get("Kebab Wrap");

                    if (quantity == null) {
                        quantity = 0;
                    }
                    quantity++;
                    list.put("Kebab Wrap", quantity);
                }
                varTotal.setText(Integer.toString(list.get("Kebab Wrap")));
                float tally = 0;
                for (Map.Entry<String, Integer> entry : list.entrySet()) {
                    String name = entry.getKey();
                    int quantity = entry.getValue();
                    double price = menu.get(name);
                    double total = quantity * price;
                    tally += total;
                    varTotalPrice.setText(Float.toString(round2(tally, 2)));

                }

            }
        });

        varIceCream.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (menu.containsKey("Ice Cream")) {
                    Integer quantity = list.get("Ice Cream");

                    if (quantity == null) {
                        quantity = 0;
                    }
                    quantity++;
                    list.put("Ice Cream", quantity);
                }
                varTotal.setText(Integer.toString(list.get("Ice Cream")));
                float tally = 0;
                for (Map.Entry<String, Integer> entry : list.entrySet()) {
                    String name = entry.getKey();
                    int quantity = entry.getValue();
                    double price = menu.get(name);
                    double total = quantity * price;
                    tally += total;
                    varTotalPrice.setText(Float.toString(round2(tally, 2)));

                }

            }
        });

        varBiscuits.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (menu.containsKey("Biscuits")) {
                    Integer quantity = list.get("Biscuits");

                    if (quantity == null) {
                        quantity = 0;
                    }
                    quantity++;
                    list.put("Biscuits", quantity);
                }
                varTotal.setText(Integer.toString(list.get("Biscuits")));
                float tally = 0;
                for (Map.Entry<String, Integer> entry : list.entrySet()) {
                    String name = entry.getKey();
                    int quantity = entry.getValue();
                    double price = menu.get(name);
                    double total = quantity * price;
                    tally += total;
                    varTotalPrice.setText(Float.toString(round2(tally, 2)));



                }

            }
        });

        varLambDoner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (menu.containsKey("Lamb Doner")) {
                    Integer quantity = list.get("Lamb Doner");

                    if (quantity == null) {
                        quantity = 0;
                    }
                    quantity++;
                    list.put("Lamb Doner", quantity);
                }
                varTotal.setText(Integer.toString(list.get("Lamb Doner")));
                float tally = 0;
                for (Map.Entry<String, Integer> entry : list.entrySet()) {
                    String name = entry.getKey();
                    int quantity = entry.getValue();
                    double price = menu.get(name);
                    double total = quantity * price;
                    tally += total;
                    varTotalPrice.setText(Float.toString(round2(tally, 2)));



                }


            }
        });


        varChickenBurger.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (menu.containsKey("Chicken Burger")) {
                    Integer quantity = list.get("Chicken Burger");

                    if (quantity == null) {
                        quantity = 0;
                    }
                    quantity++;
                    list.put("Chicken Burger", quantity);
                }
                varTotal.setText(Integer.toString(list.get("Chicken Burger")));
                float tally = 0;
                for (Map.Entry<String, Integer> entry : list.entrySet()) {
                    String name = entry.getKey();
                    int quantity = entry.getValue();
                    double price = menu.get(name);
                    double total = quantity * price;
                    tally += total;

                    varTotalPrice.setText(Float.toString(round2(tally, 2)));










                }

            }
        });


















    }

}

Below is my xml file

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/Kebab"
        android:layout_width="87dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:text="Kebab"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.028"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.262" />

    <Button
        android:id="@+id/ChickenBurger"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Chicken Burger"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.703" />

    <Button
        android:id="@+id/IceCream"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Ice Cream"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.029"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.56" />

    <Button
        android:id="@+id/Biscuits"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Biscuits"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.028"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.413" />

    <Button
        android:id="@+id/LambDoner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Lamb Doner"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.031"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.85" />

    <ScrollView
        android:id="@+id/Log"
        android:layout_width="217dp"
        android:layout_height="108dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="68dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="68dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.018">

        <LinearLayout
            android:id="@+id/Items"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/Total"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Quantity"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintHorizontal_bias="0.786"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.883" />

            <TextView
                android:id="@+id/TotalPrice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_weight="1"
                android:text="Price"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.672"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.349" />
        </LinearLayout>
    </ScrollView>

</android.support.constraint.ConstraintLayout>
  • Are you familiar with using a `RecyclerView` or a `ListView`? – user3170251 Jan 17 '19 at 18:58
  • I'm new to android studio, so I am not – QuavoHuncho Jan 17 '19 at 19:10
  • see [the answer in this link](https://stackoverflow.com/questions/40584424/simple-android-recyclerview-example). Its a great example that I sometimes reference when making recycler views. Theres also more tutorials at the bottom of that answer – user3170251 Jan 17 '19 at 19:19
  • It's exactly what I'm looking for but the problem is that I don't want to create a new activity. It will make my program harder to use. I want the list to be alongside my buttons all on the same activity. Is this possible? – QuavoHuncho Jan 17 '19 at 19:23
  • you can insert a `RecyclerView` into your existing activity. See activity_main.xml in that link. A `RecyclerView` is just another UI component. It even works with your `ConstraintLayout`. The link only created a new activity for demonstration purposes. – user3170251 Jan 17 '19 at 19:29
  • I have a scrollview with a linear layout inside. this will work yes? – QuavoHuncho Jan 17 '19 at 19:39

1 Answers1

0

You should use recycler view with list and update the list onClick then call notifyDataSetChanged on recycler view adapter to add new item.

please check this example and this

Khaled Qasem
  • 879
  • 7
  • 20