Я пытаюсь удалить один TableRow из TableLayout динамически с помощью OnLongClick. Но он удаляет все строки. Я пытался удалить по идентификатору, но каждый получил NullPointer исключениеdelete TableRow from TableLayout динамически
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)
С идентификатором, это нормально работает. 'MyRow = (TableRow) findViewById (R.id.rowselected); myTable.removeView (MyRow); '. Что вы сделали, когда вы «пытались удалить по id»? – Fllo
получил nullpointer –
надеюсь, что вы удалите родительский идентификатор вместо childId. – Yup