Java 标签需要反映所有arrayList元素的总和,只反映最新的元素
晚上好,伙计们!快速提问 我试图在我的应用程序中获得一个标签,以反映每次按下保存按钮时不断累积的一组整数的总和 此外,我想使用文件输入/输出来保存用户输入,以便将其存储到文件中并在以后检索,但这是另一次的问题 我可以让标签反映一个数字,该数字等于用户输入的最新整数,但我需要标签反映用户输入的所有整数的总和 请看一看,我不知道为什么它没有做它应该做的:Java 标签需要反映所有arrayList元素的总和,只反映最新的元素,java,android,Java,Android,晚上好,伙计们!快速提问 我试图在我的应用程序中获得一个标签,以反映每次按下保存按钮时不断累积的一组整数的总和 此外,我想使用文件输入/输出来保存用户输入,以便将其存储到文件中并在以后检索,但这是另一次的问题 我可以让标签反映一个数字,该数字等于用户输入的最新整数,但我需要标签反映用户输入的所有整数的总和 请看一看,我不知道为什么它没有做它应该做的: public View.OnClickListener onSave = new View.OnClickListener (){ @
public View.OnClickListener onSave = new View.OnClickListener (){
@Override
public void onClick(View v) {
Info r = new Info();
EditText name = (EditText) findViewById(R.id.field_transaction);
EditText address = (EditText) findViewById(R.id.field_income_expense);
TextView total= (TextView)findViewById(R.id.total);
RadioGroup types = (RadioGroup) findViewById(R.id.rgrp_types);
switch (types.getCheckedRadioButtonId()) {
case R.id.rbtn_income:
r.setType("income");
break;
case R.id.rbtn_expense:
r.setType("expense");
break;
}
r.setName(transaction.getText().toString());
r.setAddress(income_expense.getText().toString());
r.setTotal(total.getText().toString());
String data = (income_expense.getText().toString()); //To get the value of income/expense and put it in a string
try {
fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(data.getBytes()); //To put string into FOS file
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ArrayList<Integer> storage = new ArrayList<>();
String collected = null;
FileInputStream fis = null;
int process = 0;
int sum = 0;
try {
fis = openFileInput(FILENAME);
byte[] dataArray = new byte[fis.available()];
int counter= 0;
while (fis.read(dataArray) != -1){ //Loops all code inside while there is still date inside file to read
collected = new String(dataArray); //Puts data into a string
process = Integer.parseInt( collected ); //Coverts that string itno an int.
storage.add(process); //Adds that int into an ArrayList
for(int i: storage){ //Adds all elements in the current state of the ArrayList together and makes puts it in int sum
sum += i;
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
fis.close();
total.setText("Balance Total: " + sum + "Php"); //It is supposed to print the sum of all elements, but all it does is print the last input entered by user
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Log.d("FoodTripActivity", "Name: " + r.getName() + "Address: " + r.getAddress() + "Type: " + r.getType());
adapter.add(r);
}
};
你知道有什么可以退货吗?您希望fis.readdataArray做什么?在此之后,您希望dataArray包含什么?请删除保存到文件中的代码,然后重新读取。让我们先把注意力集中在第一个问题上。你添加了一个Info类的实例,为什么称它为r?到带有adapter.addr的适配器;因此,适配器具有所有值。标签在哪里?是文本视图吗?你看到你对sum什么都不做了吗?