繁体   English   中英

Android Studio-每次单击按钮时如何在线性布局上生成新的textview

[英]Android Studio - How can I generate a new textview on my linear layout every time i click a button

我有一个程序可以显示不同产品的总价和数量,例如烤肉串和鸡肉汉堡

这是问题所在。 无论叫什么东西,我都有一个线性布局(或可滚动视图),它显示数量和总价,但不是每个项目都显示。

例如,如果我单击Kebab Wrap 3次,然后单击Chicken Burger 3次,我将在可滚动视图中获得此信息:

quantity = 3
Total price = 26.94

但是,我想要的是这个出现

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

问题是所有商品都彼此共享数量和价格,这是错误的。 我需要他们展示自己独特的数量,独特的总价,然后是所有商品的总价。

因此,要明确一点:每次滚动按钮时,我想要的是可滚动视图来存储我的产品,例如Kebab Wrap。

我已经尝试了几件事,但是我不断出错。 请帮我

这些项目在哈希图中。 我将向您显示以下代码

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










                }

            }
        });


















    }

}

以下是我的xml文件

<?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>

您应该使用带有列表的回收站视图并更新列表onClick,然后在回收站视图适配器上调用notifyDataSetChanged以添加新项目。

请检查这个例子这个

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM