2013-11-28 107 views
0

我想從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) 
+0

隨着ID,它通常工作。 'MyRow =(TableRow)findViewById(R.id.rowselected); myTable.removeView(MyRow);'。當你「試圖通過ID刪除」你做了什麼? – Fllo

+0

得到空指針 –

+0

希望你刪除父Id而不是childId。 – Yup

回答

0

造成這種情況的最簡單的方法是標記要刪除的行,然後重新創建表而不添加標記的刪除行。

+0

如何?你可以給一些代碼嗎? –

0

您可以更新您長按如下,並檢查是否正常工作:

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

查看沒有方法removeView,這是一個錯誤 –

+0

@JenyaKirmiza謝謝指出,..更新我的回答 –

+0

它得到了空指針( –

0

這可能不適用於錶行工作,但你可以使用view.setVisibility(View.gone)如果沒有什麼作品。