0

I'm trying to get the individual values from an ArrayList but I haven't had any luck. It appears that when I go through the loop, it's overwriting the i Integer variable being used in the for loop.

public ArrayList<Integer> getTests() 
{
    return tests;
}


// Go through all the tests downloaded from the bluetooth module
        for (Integer i :ma.mOpacityTestResult.getTests())
        {
            View row = inflater.inflate(R.layout.test_report_row, (ViewGroup) container, false);
            TextView left = (TextView) row.findViewById(R.id.rowLeft);
            TextView right = (TextView) row.findViewById(R.id.rowRight);

            // This is the checkbox we want shown but to only worth with counter
            CheckBox check = (CheckBox) row.findViewById(R.id.checkBox);
            check.setVisibility(View.VISIBLE);
            check.setChecked(true);


            String testResultString = getString(R.string.TestNumber) + String.valueOf(counter++);

        // Load getTest Results into a list
          List<Object> list = new ArrayList<>();
          list.add(i);          // Trying to seperate each value of i download from bluetooth here but failing
        }

2 Answers 2

1

You need to keep initialization out of the loop

This should be outside the loop

List<Object> list = new ArrayList<>();

Only addition of values to list should be in the loop

list.add(i); 
Sign up to request clarification or add additional context in comments.

1 Comment

If I want to get the first element of the list by using list.get(0), it's still returning the last item of the i variable even though I've specifically selected the 0 index.
1

Your code is creating a new list on every iteration and adding the value. So at the end of final iteration, only the list created in the last iteration will be left, which gives a feeling that the value in the list is being overwritten. To fix this, the list initialization (List list = new ArrayList<>();) has to be done outside the for loop.

3 Comments

I added the List initialization outside the for loop the i variable is still getting overwritten
Can you please paste the updated code? It should ideally work.
It turns out that by using a switch statement, I wasn't somehow getting the correct values from the list but when I was using if.. statements it was working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.