2014-04-24 75 views
0
public void populate() { 

     TableRow tr = new TableRow(getActivity()); 

     tr.setId(100 + count); 
     tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT)); 

     Button b_add = new Button(getActivity()); 
     b_add.setId(20); 
     b_add.setText("+"); 
     b_add.setTextColor(Color.RED); 
     b_add.setGravity(Gravity.CENTER); 
     b_add.setPadding(2, 2, 2, 2); 
     b_add.setOnClickListener(add); 

     tr.addView(b_add); 

     TextView v_no = new EditText(getActivity()); 
     v_no.setId(200 + count); 
     v_no.setHint("Vehicale NO"); 
     v_no.setTag("adress"); 
     v_no.setTag("v_no"); 
     v_no.setTextColor(Color.BLACK); 
     v_no.setGravity(Gravity.CENTER); 
     tr.addView(v_no); 

     Button b_minus = new Button(getActivity()); 
     b_minus.setId(20); 
     b_minus.setText("-"); 
     b_minus.setTextColor(Color.RED); 
     b_minus.setGravity(Gravity.CENTER); 
     b_minus.setPadding(2, 2, 2, 2); 
     b_minus.setOnClickListener(remove); 
     tr.addView(b_minus); 
     // finally add this to the table row 
     tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT)); 
     count++; 
    } 

我创建此代码以使用表格布局创建具有三列的表格。它工作正常。但我需要删除除第一行以外的表格行。可以帮助我做到这一点删除除第一行之外的其他表格行

回答

1
while (yourTableLayout.getChildCount() > 1) 
    yourTableLayout.removeView(yourTableLayout.getChildAt(yourTableLayout.getChildCount() - 1); 

这应该遍历您TableLayout并删除这些行逐一从底部到顶部,直到仍只是一个(这将不会删除)。