从android中的表格布局动态删除行

aja*_*jay 2 android android-tablelayout

我正在TableLayout为其动态添加行.在每行中有2个元素,其中一个是TextView另一个元素Button.当我单击一行中存在的按钮时,应该删除该行.如何在Android中完成?如何查找rowid以及如何动态删除行.任何人都可以帮我解决这个问题.

Gee*_*ing 7

单击该按钮将为您提供单击视图,即您的情况下的按钮.该按钮的父级是您要删除的行.从它的父级删除该行将消除它.

一个如何实现这个的例子:

    button.setOnClickListener(new OnClickListener()
    {
        @Override public void onClick(View v)
        {
            // row is your row, the parent of the clicked button
            View row = (View) v.getParent();
            // container contains all the rows, you could keep a variable somewhere else to the container which you can refer to here
            ViewGroup container = ((ViewGroup)row.getParent());
            // delete the row and invalidate your view so it gets redrawn
            container.removeView(row);
            container.invalidate();
        }
    });
Run Code Online (Sandbox Code Playgroud)