[英]Adding to ArrayList from EditText
I am trying to implement a button that saves integers entered into an EditText
and save them into an ArrayList
. 我正在尝试实现一个按钮,该按钮可以保存输入到
EditText
整数并将其保存到ArrayList
。 I declared my ArrayList globally in my class and am calling it inside of my OnClickListener
method. 我在类中全局声明了ArrayList,并在
OnClickListener
方法中调用它。 I am unsure whether or not I am saving to this ArrayList
because I am unable to display what I have saved in said ArrayList
. 我不确定是否要保存到此
ArrayList
因为无法显示在ArrayList
保存的内容。
My declaration of the list is; 我对名单的声明是:
ArrayList<String> savedScores = new ArrayList<String >();
This is what I am using to save to my ArrayList
; 这就是我用来保存到
ArrayList
;
`savedScores.add(input1.getText().toString());`
Now, in my OnClickListener
method, I have a button that saves user input into the ArrayList
(or so I am hoping), and another to display what I have saved. 现在,在我的
OnClickListener
方法中,我有一个按钮将用户输入保存到ArrayList
(或者我希望如此),另一个按钮显示我已保存的内容。 However, when I click on the "editScore" button, the TextEdit
is cleared as if I have nothing saved in my ArrayList
. 但是,当我单击“ editScore”按钮时,
TextEdit
被清除,好像我的ArrayList
没有保存任何内容一样。 This is simply a test to see if I am properly saving to my array and any help would be much appreciated! 这只是一个测试,以查看我是否正确保存到阵列中,任何帮助将不胜感激! Thank you.
谢谢。
switch (view.getId()) {
case R.id.buttTotal:
if (blankCheck.equals("")) {
Toast blankError = Toast.makeText(getApplicationContext(), "YOU CANT SKIP HOLES JERK", Toast.LENGTH_LONG);
blankError.show();
break;
} else {
int num1 = Integer.parseInt(input1.getText().toString()); //Get input from text box
int sum = num1 + score2;
score2 = sum;
output1.setText("Your score is : " + Integer.toString(sum));
input1.setText(""); //Clear input text box
//SAVE TO THE ARRAYLIST HERE
savedScores.add(input1.getText().toString());
break;
}
case R.id.allScores: //CHANGE THIS TO AN EDIT BUTTON, ADD A HOLE NUMBER COUNTER AT TOP OF SCREEN!!!!!
output1.setText("you messed up");
break;
case R.id.editScore: //Need to set up Save Array before we can edit
output1.setText(savedScores.get(0));
break;
}
Because you are saving empty values into your ArrayList
. 因为您要将空值保存到
ArrayList
。 See here 看这里
input1.setText(""); //Clear input text box
//SAVE TO THE ARRAYLIST HERE
savedScores.add(input1.getText().toString());
The value of input1
is empty. input1
值为空。 Clear the input after you saved it to the array. 将输入保存到阵列后,清除输入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.