【问题标题】:I have a list of numbers using " private ArrayList<HashMap<String, String>> list" and I want to sum them我有一个使用“private ArrayList<HashMap<String, String>> list”的数字列表,我想对它们求和
【发布时间】:2020-01-22 14:52:29
【问题描述】:
private ArrayList<HashMap<String, String>> list;
public static final String FIRST_COLUMN="Brand";
public static final String SECOND_COLUMN="Model";
public static final String THIRD_COLUMN="Total";

public void addDeleteItemList() {

    list=new ArrayList<HashMap<String,String>>();
    final ListViewAdapter adapter0=new ListViewAdapter(this,list);
    listView.setAdapter(adapter0);

    txtInput = findViewById(R.id.cantitateID);
    txtInput2 = findViewById(R.id.specieID);
    txtInput3 = findViewById(R.id.sortimentID);
    Button but = findViewById(R.id.addID);
    but.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            HashMap<String,String> hashmap=new HashMap<String, String>();

            String  newItem = txtInput2.getSelectedItem().toString();
            String  newItem2 = txtInput3.getSelectedItem().toString();
            String  newItem3 = txtInput.getText().toString();

            hashmap.put(FIRST_COLUMN, newItem);
            hashmap.put(SECOND_COLUMN, newItem2);
            hashmap.put(THIRD_COLUMN, newItem3);
            list.add(hashmap);
            adapter0.notifyDataSetChanged();
            int sum = 0;
            for (int i=0; i<list.size();i++){
                sum+= list.get(i);
            }
            et_volumTotal.setText(String.valueOf(sum));

        }
    });
}

错误:二元运算符 '+' 第一种类型的操作数类型错误:int 第二种:HashMap

【问题讨论】:

  • 您不能使用哈希图进行计算。正确地说你收到了第一个 int 和另一个 hashmap
  • 我如何计算总和?
  • 通过查看您的图像,我在您的哈希图中没有看到任何整数我可以说您的哈希图键可能是斯柯达,值是汽车名称
  • 请解释您的问题意味着您要添加的内容。

标签: android arraylist hashmap


【解决方案1】:

首先你必须从HashMap 获取THIRD_COLUMN,然后将其转换为int。之后你就可以总结了。检查以下:

int sum = 0;
for (int i=0; i<list.size();i++) { 

    int value = 0;
    try {
        value = Integer.parseInt(list.get(i).get(THIRD_COLUMN));
    } catch(Exception ex) {
        ex.printStackTrace();
    }

    sum += value;
}

【讨论】:

  • 非常感谢先生!它完美地工作。对不起,如果我没有正确表达自己
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-04
  • 1970-01-01
  • 2014-07-22
  • 2017-05-08
  • 2017-08-30
相关资源
最近更新 更多