Jenya Kirmiza
Jenya Kirmiza

Reputation: 711

delete TableRow from TableLayout dynamically

i'm trying to delete one TableRow from the TableLayout dynamically by OnLongClick. But it deletes all rows. I tried to delete by id, but everytime got nullpointer exception

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 i don't know what's wrong

<?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>

log

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)

Upvotes: 0

Views: 1079

Answers (3)

Renato Probst
Renato Probst

Reputation: 6054

This may not work for table rows, but you can just use view.setVisibility(View.gone) if nothing works.

Upvotes: 0

Amulya Khare
Amulya Khare

Reputation: 7698

Can you update your long click as follows and check if it works:

@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;
}

Upvotes: 1

Ayman Mahgoub
Ayman Mahgoub

Reputation: 4220

The easiest way for this, is to mark the row to be deleted and then re-create the table without adding the marked deleted row.

Upvotes: 0

Related Questions