需要学习资源,请点击开挂教学楼
I'm trying to create an array of TextViews that each get populated from an array of objects, but each TextView ends up displaying the last object that I enter.
I've initiated the array of TextViews with
TextView[] loanViewDisplay = new TextView[5];
and inside of my onCreate, I have
loanViewDisplay[0] = findViewById(R.id.loanView1);
loanViewDisplay[1] = findViewById(R.id.loanView2);
loanViewDisplay[2] = findViewById(R.id.loanView3);
loanViewDisplay[3] = findViewById(R.id.loanView4);
loanViewDisplay[4] = findViewById(R.id.loanView5);
I have an addNewLoan button that initiates an Intent and startActivityForResult. The new loan gets added to a array called arrayOfLoans and the program returns to this activity where I want to list each loan from arrayOfLoans in its own TextView.
for(int i=0; i<listOfLoans.length; i++){
if(arrayOfLoans[i] != null){
loanViewDisplay[i].setText(arrayOfLoans[i].loanInformation());
}
}
If I add one loan with a principle of $5000, the output is perfect with
Loan 1: Principle $5000
but if I then click my addNewLoan button and add a second loan with a value of $2000, my output turns into
Loan 2: Principle $2000
Loan 2: Principle $2000
I know my data is being added to arrayOfLoans properly. I've narrowed the problem down to the array of TextViews. Thanks for the help