从数组列表中<Integer>获取单个值



我正试图从ArrayList中获取单个值,但运气不佳。当我遍历循环时,它似乎覆盖了for循环中使用的I Integer变量。

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
}

您需要将初始化保持在循环之外

这应该在循环之外

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

只有添加到列表的值才应该在循环中

list.add(i); 

您的代码在每次迭代中创建一个新列表并添加值。因此,在最后一次迭代结束时,只剩下上次迭代中创建的列表,这会给人一种列表中的值被覆盖的感觉。要解决此问题,请执行列表初始化(list-list=newArrayList<>((;(必须在for循环之外完成。

相关内容

最新更新