• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps TableLayout in loop

LeonR

Newbie
Ok, im hoping this will be quite an easy thing for somebody to answer..

This works :-

Code:
public void updateDisplay() {
      
   item=0;
    
    myTableLayout.removeAllViews();
    

        
       
        TableRows[item].addView(ProgressBars[item]);
        myTableLayout.addView(TableRows[item]);
        
item++;

         TableRows[item].addView(ProgressBars[item]);
          myTableLayout.addView(TableRows[item]);
All I am doing is building up the tablelayout with my rows... this add's two progress bars to my layout.



When I introduce a loop... it only works on the first item, then crashes :(

Code:
public void updateDisplay() {
     
      TableLayout  myTableLayout = new TableLayout(this);
   
    
    myTableLayout.removeAllViews();
    
    for (item=0;item<ItemCount;item++ ) {
        
        TableRows[item].removeAllViews();
        
        TableRows[item].addView(ProgressBars[item]);
        
        myTableLayout.addView(TableRows[item]);
        
        
    }
I don't understand why it crashes using the loop idea? I have a feeling whats happening is I call this method the first time, and it works as its the first time (item 0) is used, but the second time I call it the loop has to do item 0 and 1, so it crashes trying to add the 1st one again (because maybe I need to unload it first?)

Everytime I click a button on the form, 'ItemCount' is incremented by 1, and this method is called, so it adds one progress bar everytime the button is clicked. The progressbar etc are declared before this method is called.

It's a little hard to explain, but any ideas are welsome :D
 
I played about with the code a little, I think the problem is im trying to reuse something thats not been cleared...

For example if I simply do this:-

Code:
 TableRows[0].addView(ProgressBars[0]);
 myTableLayout.addView(TableRows[0]);

It works once, but when I run the same code again, it crashes. But i'm not sure how to 'reset' everything, as I want to rebuild the entire view everytime this method is called... ive tried 'removeAllViews' , and that does do what its meant to, but it does not fix the problem.

How can I 'destroy' an object, such as TableRows[0].destroy ?:confused:
 
Back
Top Bottom