我的索引超出范围异常并且无法找出原因,请帮助我。我正在尝试遍历列表。列表大小为 2。我正在尝试创建动态表并打印列表。
public void display(ArrayList list) {
TableLayout tl = (TableLayout) findViewById(R.id.main_table);
for(int i = 0;i<=list.size(); i++){
EmployeeBean empList = (EmployeeBean) list.get(i);
int count = 0;
TableRow tr = new TableRow(this);
tr.setId(100+count);
tr.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
tr.setClickable(true);
final TextView empName = new TextView(this);
empName.setId(200+count);
empName.setText(empList.getName());
tr.addView(empName);
final TextView empStream = new TextView(this);
empStream.setId(200+count);
empStream.setText(empList.getStream());
tr.addView(empStream);
final TextView empLevel = new TextView(this);
empLevel.setId(200+count);
empStream.setText(empList.getLevel());
final TextView empDesignation = new TextView(this);
empDesignation.setId(200+count);
empDesignation.setText(empList.getDesignation());
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
count++;
tr.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
outpuText = new TextView(MainActivity.this);
outpuText.setText("Name: " + empName.getText() + "\n"
+ "Stream: " + empStream.getText() + "\n" + "Level: "
+ empLevel + "\n" + "Designation :" + empDesignation);
setContentView(outpuText);
}
});
}
我收到的例外是
java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2