2012-10-30 111 views
1

我正在創建一個小應用程序,您可以在其中通過按鈕單擊來添加或刪除表格行。每當我點擊「添加行」按鈕時,該按鈕將添加一個表格行到佈局,並做它應該做的事情。但是,「刪除行」不會刪除新添加的表格行,但它確實會減少(int count)。我已經工作了4到5個小時,現在我沒有運氣。這裏的代碼:無法從佈局中刪除視圖

int count = 0; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_main); 
    Button buttonAdd = (Button) findViewById(R.id.button1); 
    Button buttonDel = (Button) findViewById(R.id.button2); 
    buttonAdd.setOnClickListener(this); 
    buttonDel.setOnClickListener(this); 
} 



public void onClick(View v) { 
    TableLayout tableLayout1; 
    EditText editText1; 
    EditText editText2; 
    TableRow tempRow; 
    EditText tempText1; 
    EditText tempText2; 
    TableRow tableRow1; 

    tableLayout1 = (TableLayout) findViewById(R.id.tableLayout1); 
    editText1 = (EditText) findViewById(R.id.editText1); 
    editText2 = (EditText) findViewById(R.id.editText2); 
    tempRow = new TableRow(MainActivity.this); 
    tempText1 = new EditText(MainActivity.this); 
    tempText2 = new EditText(MainActivity.this); 
    tempText1.setInputType(InputType.TYPE_CLASS_TEXT); 
    tempText2.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL); 
    tableRow1 = (TableRow) findViewById(R.id.tableRow1); 

    tempRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
    tempText1.setLayoutParams(editText1.getLayoutParams()); 
    tempText2.setLayoutParams(editText2.getLayoutParams()); 
    switch(v.getId()){ 
    case R.id.button1: 
     if(count != 9){ 
      count++; 

      tempRow.addView(tempText1); 
      tempRow.addView(tempText2); 
      tableLayout1.addView(tempRow); 
     } else { 
      final AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create(); //Read Update 
      alertDialog.setTitle("Error"); 
      alertDialog.setMessage("You can only have 10 rows!"); 

      alertDialog.setButton("Ok", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        alertDialog.dismiss(); 
       } 
      }); 

      alertDialog.show(); 
     } 
     break; 
    case R.id.button2: 
     if(count != 0){ 
      count--; 
      tempRow.removeView(tempText1); 
      tempRow.removeView(tempText2); 
      tableLayout1.removeView(tempRow); 
      //tableLayout1.invalidate(); 

     } else { 
      final AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create(); //Read Update 
      alertDialog.setTitle("Error"); 
      alertDialog.setMessage("You must have at least one row!"); 

      alertDialog.setButton("Ok", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        alertDialog.dismiss(); 
       } 
      }); 

      alertDialog.show(); 
     } 
     break; 
     default: 
      //Do something; 
    } 

}  

10-30 21:22:23.379: E/AndroidRuntime(3970): FATAL EXCEPTION: main 
10-30 21:22:23.379: E/AndroidRuntime(3970): java.lang.NullPointerException 
10-30 21:22:23.379: E/AndroidRuntime(3970): at com.android.hopegpacalc.MainActivity.onClick(MainActivity.java:57) 
10-30 21:22:23.379: E/AndroidRuntime(3970): at android.view.View.performClick(View.java:3511) 
10-30 21:22:23.379: E/AndroidRuntime(3970): at android.view.View$PerformClick.run(View.java:14105) 
10-30 21:22:23.379: E/AndroidRuntime(3970): at android.os.Handler.handleCallback(Handler.java:605) 
10-30 21:22:23.379: E/AndroidRuntime(3970): at android.os.Handler.dispatchMessage(Handler.java:92) 
10-30 21:22:23.379: E/AndroidRuntime(3970): at android.os.Looper.loop(Looper.java:137) 
10-30 21:22:23.379: E/AndroidRuntime(3970): at android.app.ActivityThread.main(ActivityThread.java:4424) 
10-30 21:22:23.379: E/AndroidRuntime(3970): at java.lang.reflect.Method.invokeNative(Native Method) 
10-30 21:22:23.379: E/AndroidRuntime(3970): at java.lang.reflect.Method.invoke(Method.java:511) 
10-30 21:22:23.379: E/AndroidRuntime(3970): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
10-30 21:22:23.379: E/AndroidRuntime(3970): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
10-30 21:22:23.379: E/AndroidRuntime(3970): at dalvik.system.NativeStart.main(Native Method) 

回答

2

這是組織的問題,試試這個:

public void onClick(View v) { 
    // These should be class variables, like count, you don't need to re-find it on every click 
    TableLayout tableLayout1 = (TableLayout) findViewById(R.id.tableLayout1); 
    EditText editText1 = (EditText) findViewById(R.id.editText1); 
    EditText editText2 = (EditText) findViewById(R.id.editText2); 

    switch(v.getId()){ 
    case R.id.button1: 
     if(count != 9){ 
      count++; 

      // Create the row only when the add button is clicked 
      TableRow tempRow = new TableRow(MainActivity.this); 
      EditText tempText1 = new EditText(MainActivity.this); 
      EditText tempText2 = new EditText(MainActivity.this); 
      tempText1.setInputType(InputType.TYPE_CLASS_TEXT); 
      tempText2.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL); 

      tempRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
      tempText1.setLayoutParams(editText1.getLayoutParams()); 
      tempText2.setLayoutParams(editText2.getLayoutParams()); 

      tempRow.addView(tempText1); 
      tempRow.addView(tempText2); 
      tableLayout1.addView(tempRow); 
     } else { 
      // Consider using Activity's showDialog() method, to reuse this "error" dialog rather than create a new one every time 
      final AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create(); //Read Update 
      alertDialog.setTitle("Error"); 
      alertDialog.setMessage("You can only have 10 rows!"); 

      alertDialog.setButton("Ok", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        alertDialog.dismiss(); 
       } 
      }); 

      alertDialog.show(); 
     } 
     break; 
    case R.id.button2: 
     if(count != 0){ 
      // Remove the last row at count or use "tableLayout1.getChildCount() - 1" 
      tableLayout1.removeView(tableLayout1.getChildAt(count)); 
      count--; 
     } else { 
      // Consider reusing the other "error" Dialog and simply changing the message 
      final AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create(); //Read Update 
      alertDialog.setTitle("Error"); 
      alertDialog.setMessage("You must have at least one row!"); 

      alertDialog.setButton("Ok", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        alertDialog.dismiss(); 
       } 
      }); 

      alertDialog.show(); 
     } 
     break; 
    default: 
     //Do something; 
    } 
} 
+0

你先生是個天才。謝謝!我現在遇到的唯一問題是,在添加一行後,刪除該行,然後嘗試再次添加一行,在行57上收到一個NullPointerException。它將這個異常指向「tempText1.setLayoutParams(editText1.getLayoutParams());」 – user1460211

+0

這很奇怪,可以編輯所有的LogCat錯誤到你的問題?所以我可以看到發生了什麼。 – Sam

+0

我可能已經排除了錯誤的行,試試這個 – Sam

0

每次onClick執行時,您的代碼都會生成一個新的TableRow。您只應在添加一行時執行此操作。如果要刪除一個,則必須使用findViewById(id)在現有視圖中找到要刪除的行,然後在該對象上運行removeView

​​

這並不意味着你必須設置在創建它的每個錶行的ID,使用setId,並跟蹤它。

0

你移除代碼爲「刪除」臨時視圖(如tempText1),實際上是沒有在表中 - 他們提出了在飛行中onClick方法。除非在進入onClick後立即實例化所有這些對象,否則請在每個switch語句的主體中執行此操作。對於添加按鈕,只需複製並粘貼即可。對於刪除人員,請將這些對象設置爲與要刪除的行相同。

看起來您似乎沒有辦法選擇要刪除的行,但假設您在其他位置執行此操作,則可以使用TableLayout.removeViewAt(int index)來選擇哪一行。如果你有視圖本身而不是索引,也有TableLayout.removeView(View view)