2014-11-08 82 views
0

我使用以下代碼從我的tablelayout刪除行;
System.out.println(vi);給我4個獨特的行,這是正確的,但爲什麼我的循環不是一次刪除所有行。我需要點擊3次。從TableLayout以編程方式刪除所有行

此代碼位於myOnclickHandler;

public void onClick(View v) { 

    TableLayout container = (TableLayout) v.getParent().getParent(); 

    int childcount = container.getChildCount(); 
    View vi; 

    for (int i = 0; i < childcount; i++) { 

      vi = container.getChildAt(i); 
      container.removeView(vi); 


      System.out.println(vi); 
    } 


} 
+0

你有沒有檢查子女數值? – 2014-11-08 11:36:26

+0

改爲嘗試container.removeAllViews()。 – user2203031 2014-11-08 11:44:52

+0

是的,所以我說我得到了4個獨特的行作爲結果 – 2014-11-08 11:48:36

回答

0

嘗試:

row = (TableRow)findViewById(R.id.row); 
    table.removeView(row); 
+0

我的佈局是以編程方式創建的,thx – 2014-11-08 12:14:54

0

利用這一點,只保留標題行。

if (table.getRootView() != null) { 
    int i = 1; 
    while (table.getChildCount() != 1) { 
     table.removeViewAt(i); 
    } 
}