我想從OnLongClick動態地從TableLayout中刪除一個TableRow。但它會刪除所有行。我試圖通過ID刪除,但每次得到空指針異常從TableLayout動態刪除TableRow
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)
隨着ID,它通常工作。 'MyRow =(TableRow)findViewById(R.id.rowselected); myTable.removeView(MyRow);'。當你「試圖通過ID刪除」你做了什麼? – Fllo
得到空指針 –
希望你刪除父Id而不是childId。 – Yup