1. Check out our companion app, Forums for Android! Download from Google Play

TableLayout in loop

Discussion in 'Application Development' started by LeonR, Mar 7, 2010.

  1. LeonR

    LeonR Member
    Thread Starter
    15

    Feb 22, 2010
    18
    0
    15
    Ok, im hoping this will be quite an easy thing for somebody to answer..

    This works :-

    Code (Text):
    1.  
    2.  
    3. public void updateDisplay() {
    4.      
    5.    item=0;
    6.    
    7.     myTableLayout.removeAllViews();
    8.    
    9.  
    10.        
    11.        
    12.         TableRows[item].addView(ProgressBars[item]);
    13.         myTableLayout.addView(TableRows[item]);
    14.        
    15. item++;
    16.  
    17.          TableRows[item].addView(ProgressBars[item]);
    18.           myTableLayout.addView(TableRows[item]);
    19.  
    20.  
    21.  
    22.  
    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 (Text):
    1.  
    2. public void updateDisplay() {
    3.      
    4.       TableLayout  myTableLayout = new TableLayout(this);
    5.    
    6.    
    7.     myTableLayout.removeAllViews();
    8.    
    9.     for (item=0;item<ItemCount;item++ ) {
    10.        
    11.         TableRows[item].removeAllViews();
    12.        
    13.         TableRows[item].addView(ProgressBars[item]);
    14.        
    15.         myTableLayout.addView(TableRows[item]);
    16.        
    17.        
    18.     }
    19.  
    20.  
    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
     

    Advertisement

  2. LeonR

    LeonR Member
    Thread Starter
    15

    Feb 22, 2010
    18
    0
    15
  3. LeonR

    LeonR Member
    Thread Starter
    15

    Feb 22, 2010
    18
    0
    15
    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 (Text):
    1.  
    2.  
    3.  
    4.  TableRows[0].addView(ProgressBars[0]);
    5.  myTableLayout.addView(TableRows[0]);
    6.  
    7.  
    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 ?:thinking:
     

Share This Page

Loading...