简体   繁体   中英

Java - adding existing objects to view iterating through an array list

what I want to do is press a button to add previously declared TableRow objects (refers to TableRow objects already created in XML file) that I have hidden using table.removeView(row1) etc on program start.

Then I want to be able to click a button to add each TableRow back to the view one at a time until all the rows are visible again. I have tried a few different ways without any luck and the last method I tried was creating an array list in my onCreate method like so:

final ArrayList<TableRow> rowlist = new ArrayList<TableRow>();
rowlist.add(row4);
rowlist.add(row5);
rowlist.add(row6);
rowlist.add(row7);
rowlist.add(row8);
rowlist.add(row9);
rowlist.add(row10);

Then trying to iterate through like this:

public void onClick(View v) {
Iterator<TableRow> rowiterator = rowlist.iterator();
while (rowiterator.hasNext()) {
table.addView(rowiterator.next());
}
}

What I get now is when I press my button it just adds all the rows back in at once, when I want it to iterate through the list adding rows one at a time.

Can anyone help me resolve this problem, or tell me if I'm being a complete idiot and suggest an entirely new and better method of achieving what I want to achieve?

Note: I'm pretty new to Java programming and on this problem I am absolutely stumped!

You're iterating over the entire array when you click the button and adding them all back in. Something like this is probably more like what you want:

public void onClick(View v) {
  if(rowlist.size() > 0)
  {
     table.addView(rowlist.get(0));
     rowlist.remove(0);
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM