0

I am trying to implement a button that saves integers entered into an EditText and save them into an ArrayList. I declared my ArrayList globally in my class and am calling it inside of my OnClickListener method. 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.

My declaration of the list is;

ArrayList<String> savedScores = new ArrayList<String >();

This is what I am using to save to my 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. However, when I click on the "editScore" button, the TextEdit is cleared as if I have nothing saved in my 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;
        }

1 Answer 1

2

Because you are saving empty values into your 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. Clear the input after you saved it to the array.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.