【问题标题】:Android update sum of value on each action madeAndroid更新每个动作的价值总和
【发布时间】:2018-10-01 10:54:14
【问题描述】:

我正在做一个应用程序,其中我的文本视图会根据选中的复选框和单选按钮立即更新。我有 3 个哈希图来存储每个项目的值:

    hashMap.put("chkCheese", 2.50);
    hashMap.put("chkPep", 3.50);
    hashMap.put("chkChick", 2.00);
    hashMap.put("chkBeef", 4.00);
    hashMap.put("chkBlackOlives", 2.00);
    hashMap.put("chkPine", 1.00);
    hashMap.put("chkMushroom", 1.00);

这是我的功能,用于显示我的复选框更改时选中的项目:

    private void checkEnoughAndMakeDisabled (CheckBox checkBoxes []) {
    int count = 0;
    for (CheckBox cb:checkBoxes) {
        cb.setEnabled(true);
        if (cb.isChecked()) {
            count++;
            toppingsSelection.add(cb);

        }
    }

    String text="";
    for (CheckBox items: toppingsSelection) {
        text = text + items.getText().toString() + ", ";
    }
    toppings.setText(text);

    if (count >= 5) {
        for (CheckBox cb:checkBoxes) {
            if (!cb.isChecked()) {
                cb.setEnabled(false);
            }
        }
    }
    toppingsSelection.clear();
}

在 onCreate() 中调用我的函数:

        //Display TOPPINGS text on each action made
    final CompoundButton.OnCheckedChangeListener chgListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            checkEnoughAndMakeDisabled(checkBox);
        }
    };

    for (CheckBox tmpCheckBox: checkBox) {

        tmpCheckBox.setOnCheckedChangeListener(chgListener);
    }

到目前为止,我只能显示检查项目的名称。我试图为我的 TextView priceSum 做类似的事情以显示总和,但它不起作用。我还尝试将计算和哈希图标签检索放在 checkEnoughAndMakeDisabled 函数中,它崩溃了。您认为获取哈希图的值并进行计算的最佳方法是什么? 我的尝试:

       //Display PRICE text on each action made
    price.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
            double cal=0.0;
            for (int y=0;y<checkBox.length;y++) {
                if (checkBox[y].isChecked()) {
                    toppingsSelection.add(checkBox[y]);
                }
            }
            for (CheckBox tmpTopping:toppingsSelection) {
                double toppingPriceSum = hashMap.get(tmpTopping.getTag());
                cal = cal + toppingPriceSum;
                String s = "RM " + toppingPriceSum;
                price.setText(s);
            }
            return false;
        }
    });

【问题讨论】:

    标签: java android checkbox hashmap


    【解决方案1】:

    也许最好在地图内使用 id 作为键 - 比如:

    val map = hashMapOf<Int, Double>()
    map[R.id.chkCheese] = 2.50
    map[R.id.chkPep] = 3.50
    ...
    

    在你的 checkEnoughAndMakeDisabled 里面:

    var text = ""
    var price = 0.0
    toppingsSelection.forEach {
        text += it.text
        price += map[it.id] ?: 0.0
    }
    

    对不起,不是用java,但我想你会明白的

    UPD:

    Java:

    String text = "";
    double price = 0;
    for (CheckBox checkBox : toppingsSelection) {
        text += checkBox.getText();
        price += map.get(checkBox.getId()) != null ? map.get(checkBox.getId()) : 0;
    }
    

    【讨论】:

    • 对不起,我不太明白这两行text += it.text price += map[it.id] ?: 0.0你是说它循环遍历toppingsSelection数组中的项目并获取hasmap中对应的id来获取并添加值?跨度>
    【解决方案2】:

    每次用户点击时,您都不需要循环,而是使用 1 种方法为您保存数据,然后用它发送布尔值和值,例如:

    int total_price = 0 ;
    private void UpdatePrice(boolean incremental,int value){
        if (incremental){
            total_price = total_price + value;
            Toast.makeText(this, String.valueOf(total_price), Toast.LENGTH_SHORT).show();
        }else {
            total_price = total_price - value;
            Toast.makeText(this, String.valueOf(total_price), Toast.LENGTH_SHORT).show();
        }
    }
    

    每次点击UpdatePrice(true,3);时调用这个方法

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-16
      • 2012-09-11
      • 2014-04-20
      • 1970-01-01
      • 1970-01-01
      • 2017-03-26
      • 1970-01-01
      相关资源
      最近更新 更多