在android中从表布局中动态删除行

在android中从表布局中动态删除行,android,android-tablelayout,Android,Android Tablelayout,我有一个表格布局,我为它动态添加了行。每行中有两个元素,一个是TextView,另一个是按钮。当我单击一行中的按钮时,该行应该被删除。在安卓系统中如何做到这一点?如何查找rowid以及如何动态删除行。有人能帮我整理一下这个问题吗。单击按钮将显示单击的视图,即您案例中的按钮。该按钮的父级是要删除的行。从其父行中删除该行将删除该行 您如何实现此功能的示例: button.setOnClickListener(new OnClickListener() { @Overr

我有一个
表格布局
,我为它动态添加了行。每行中有两个元素,一个是
TextView
,另一个是
按钮
。当我单击一行中的按钮时,该行应该被删除。在安卓系统中如何做到这一点?如何查找rowid以及如何动态删除行。有人能帮我整理一下这个问题吗。

单击按钮将显示单击的视图,即您案例中的按钮。该按钮的父级是要删除的行。从其父行中删除该行将删除该行

您如何实现此功能的示例:

    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();
        }
    });

您需要为动态添加行分配ID,使用该ID可以获取特定行的值,也可以删除单击“行”按钮的行

在onCreate()中:-

表格布局:-

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:id="@+id/parent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/add"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TableLayout
            android:id="@+id/table1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </TableLayout>
    </LinearLayout>

</ScrollView>

显示您的代码?到目前为止你做了什么?我们需要知道您是如何实现listLook的:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:id="@+id/parent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/add"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TableLayout
            android:id="@+id/table1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </TableLayout>
    </LinearLayout>

</ScrollView>