2011-10-18 31 views
0

我被Android SDK API弄糊塗了。 我調用了GroupView.RemoveAllViews來清理所有的子視圖,但是UI不刷新。 順便說一下,我嘗試了RemoveViewAt(0)幾次,併發生NullReference異常,這意味着子視圖被刪除,但用戶界面不會改變。 任何幫助將不勝感激。非常感謝。Android 2.2中的RemoveAllViews方法

這是源代碼的一部分。

private TableLayout contentTable; 

    @Override 
    protected void onLayout(boolean changed, int l, int t, int r, int b) { 
     contentTable = new TableLayout(this.getContext()); 
     for(int i = 0 ; i < 10 ; i ++) 
     { 
     TableRaw tr = new TableRaw(this.getContext()); 
     Button b = new Button(this.getContext()); 
     b.setOnClickListener(new OnClickListener(){ 
       public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Clean() 
      } 
     }); 
     tr.addView(b); 
     contentTable.addView(tr); 
     } 
    super.onLayout(changed, l, t, r, b); 
    } 

    private void Clean() 
    { 
     contentTable.removeAllViews(); 
     this.invalidate(); 
    } 

回答

1

您需要使佈局無效。使用invalidate()方法到您的GroupView。

GroupView.invalidate() 

從我從您的意見得到。

我相信你的主要活動如下。

public class YourActivity extends Activity { 
    testView yourviewGroup; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     yourviewGroup = (testView)findViewById(R.id.yourid); 

     /**if you want to clean your viewgroup **/ 
     yourviewGroup.Clean(); 
     yourviewGroup.invalidate(); 
    } 
} 
+0

我曾經嘗試this.invalidate()和contentTable.invalidate(),但它不工作 – edward

+0

我認爲u的把這個自定義視圖(表格佈局)到您的主要活動。如果是這樣,無效需要從我們的主要活動調用。不是從這個自定義視圖文件。 – PH7

+0

不完全。我自己生成了GroupView,並將該視圖用作xml文件中的標記,並說。 testView擴展了GroupView。這在ViewGroup中被調用。 – edward