Java Android更新每个操作的值总和
我正在做一个应用程序,其中我的文本视图会根据选中的复选框和单选按钮立即进行自我更新。我有3个哈希映射来存储每个项目的值:Java Android更新每个操作的值总和,java,android,checkbox,hashmap,Java,Android,Checkbox,Hashmap,我正在做一个应用程序,其中我的文本视图会根据选中的复选框和单选按钮立即进行自我更新。我有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); hashMa
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()中调用我的函数:
到目前为止,我只能显示选中项目的名称。我试图为我的TextView priceSum做类似的事情来显示总金额,但它不起作用。我还尝试将计算和hashmap标记检索放在checkEnoughAndMakeDisabled函数中,结果崩溃了。您认为获取hashmap值并进行计算的最佳方法是什么?
我的尝试:
//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;
}
});
//显示每个操作的价格文本
price.setOnEditorActionListener(新的TextView.OnEditorActionListener(){
@凌驾
公共布尔onEditorAction(TextView TextView、int i、KeyEvent KeyEvent){
双标定=0.0;
对于(int y=0;y用户每次单击时,您不需要循环,而是创建1个方法为您保存数据,并发送boolen和value,例如:
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)时调用这个方法;
也许最好在map-like中使用ID键:
val map = hashMapOf<Int, Double>()
map[R.id.chkCheese] = 2.50
map[R.id.chkPep] = 3.50
...
对不起,不是java,但我想你会理解的
UPD:
爪哇:
抱歉,我不太理解这两行内容
text+=it.text price+=map[it.id]?:0.0
您的意思是它循环遍历ToppingSelection数组中的项目并在hasmap中获取相应的id以获取并添加值吗?更新答案。
val map = hashMapOf<Int, Double>()
map[R.id.chkCheese] = 2.50
map[R.id.chkPep] = 3.50
...
var text = ""
var price = 0.0
toppingsSelection.forEach {
text += it.text
price += map[it.id] ?: 0.0
}
String text = "";
double price = 0;
for (CheckBox checkBox : toppingsSelection) {
text += checkBox.getText();
price += map.get(checkBox.getId()) != null ? map.get(checkBox.getId()) : 0;
}