tablelayout delete tablerow in android

[亡魂溺海] 提交于 2019-12-06 16:05:39

问题


    final TableLayout table = (TableLayout) findViewById(R.id.tableLayout);
    TableRow row = new TableRow(this);


    TextView t2 = new TextView(this);
    t2.setText("test");
    row.addView(t2);

    Button bu = new Button(this);
    bu.setBackgroundResource(R.drawable.del);
    bu.setOnClickListener(new Button.OnClickListener() {

        @Override
        public void onClick(View v) {

            //I need to delete the tablerow
                            //how to do?

        }

    });
    row.addView(bu);

    table.addView(row, new TableLayout.LayoutParams(WC, WC));

** i want to delete tablerow in bu.setOnClickListener how to do removeViewAt() ,i cant find indexId **


回答1:


use removeView for removing tablerow as:

table.removeView(row);

NOTE: If they don't have unique id then use:

table.removeView(rowIndex);

and by using removeViewAt

for(int i = 0, j < table.getChildCount(); i < j; i++){ 
    // then, you can remove the the row you want... 
    // for instance... 
    TableRow row = getChildAt(i); 
    if( something you want to check ) { 
        removeViewAt(i); 
        // or... 
        removeView(row); 
    } 
} 


来源:https://stackoverflow.com/questions/10515963/tablelayout-delete-tablerow-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!