从TableLayout动态删除TableRow

时间:2013-11-28 10:26:41

标签: android

我正试图通过OnLongClick动态地从TableLayout中删除一个TableRow。但它会删除所有行。我试图通过id删除,但每次都得到nullpointer异常

for (Entry<String, User> entry : users_.entrySet()) {

        item = factory.inflate(R.layout.row_table, null);
        tableRow = (TableRow) item.findViewById(R.id.TableRow1);
        tableRow.setClickable(true);
        tableLayout.addView(item, new TableLayout.LayoutParams(
                                TableLayout.LayoutParams.FILL_PARENT,
                                TableLayout.LayoutParams.WRAP_CONTENT));
                tableRow.setOnLongClickListener(new OnLongClickListener() {

                            @Override
                            public boolean onLongClick(View v) {
                                // TODO Auto-generated method stub
                                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();
                                return false;
                            }
                        });
    }

row_table.xml 我不知道出了什么问题

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/TableRow1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#DEDEDE" >



    <ImageView
        android:id="@+id/imageStatus"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/cell_shape"/>

    <TextView
        android:id="@+id/textVuezd"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@drawable/cell_shape"
        android:text=""
        android:textSize="6dp" />

    <TextView
        android:id="@+id/textOtkuda"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@drawable/cell_shape"
        android:text=""
        android:textSize="6dp" />

    <TextView
        android:id="@+id/textKuda"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@drawable/cell_shape"
        android:text=""
        android:textSize="6dp" />

    <TextView
        android:id="@+id/textSeatsPrice"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@drawable/cell_shape"
        android:text=""
        android:textSize="6dp" />

</TableRow>

日志

11-28 13:17:08.954: E/AndroidRuntime(26406): FATAL EXCEPTION: main
11-28 13:17:08.954: E/AndroidRuntime(26406): java.lang.NullPointerException
11-28 13:17:08.954: E/AndroidRuntime(26406):    at android.view.View.showContextMenu(View.java:4210)
11-28 13:17:08.954: E/AndroidRuntime(26406):    at android.view.View.performLongClick(View.java:4174)
11-28 13:17:08.954: E/AndroidRuntime(26406):    at android.view.View$CheckForLongPress.run(View.java:17064)
11-28 13:17:08.954: E/AndroidRuntime(26406):    at android.os.Handler.handleCallback(Handler.java:615)
11-28 13:17:08.954: E/AndroidRuntime(26406):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-28 13:17:08.954: E/AndroidRuntime(26406):    at android.os.Looper.loop(Looper.java:155)
11-28 13:17:08.954: E/AndroidRuntime(26406):    at android.app.ActivityThread.main(ActivityThread.java:5520)
11-28 13:17:08.954: E/AndroidRuntime(26406):    at java.lang.reflect.Method.invokeNative(Native Method)
11-28 13:17:08.954: E/AndroidRuntime(26406):    at java.lang.reflect.Method.invoke(Method.java:511)
11-28 13:17:08.954: E/AndroidRuntime(26406):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
11-28 13:17:08.954: E/AndroidRuntime(26406):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
11-28 13:17:08.954: E/AndroidRuntime(26406):    at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:1)

您可以按如下方式更新长按,并检查它是否有效:

@Override
public boolean onLongClick(View v) {
    // TODO Auto-generated method stub
    TableLayout container = (TableLayout) v.getParent();
    // delete the row and invalidate your view so it gets
    // redrawn
    container.removeView(v);
    container.invalidate();
    return false;
}

答案 1 :(得分:0)

最简单的方法是标记要删除的行,然后重新创建表而不添加标记的已删除行。

答案 2 :(得分:0)

这可能不适用于表行,但如果没有任何效果,您可以使用view.setVisibility(View.gone)。