Java 无法从布局中删除视图

Java 无法从布局中删除视图,java,android,layout,view,Java,Android,Layout,View,我正在创建一个小应用程序,您可以通过单击按钮来添加或删除表行。每当我单击“添加行”按钮时,该按钮都会向布局中添加一个表行,并执行它应该执行的操作。然而,“delete line”并没有删除新添加的表行,而是像预期的那样递减(int count)。我已经在上面搜索了大约4到5个小时,现在我没有运气了。代码如下: int count = 0; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInst

我正在创建一个小应用程序,您可以通过单击按钮来添加或删除表行。每当我单击“添加行”按钮时,该按钮都会向布局中添加一个表行,并执行它应该执行的操作。然而,“delete line”并没有删除新添加的表行,而是像预期的那样递减(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;
    }

}    


每次执行onClick时,代码都会生成一个新的
TableRow
。仅当要添加行时才应执行此操作。如果要删除一行,则必须使用
findViewById(id)
在现有视图中查找要删除的行,然后在该对象上运行
removeView

case R.id.button2:
    if ( count != 0 ) {
        count--;
        tempRow = (TableRow)findViewById( idoftablerowtodelete );
        tableLayout1.removeView( tempRow );

这意味着您必须在创建表行时,使用
setId
,在每个表行上设置一个id,并对其进行跟踪。

这是一个组织问题,请尝试以下方法:

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;
    }
}
您的删除代码是“删除”临时视图(例如
tentext1
),它们实际上不在表中-它们是在
onClick
方法中动态创建的。不要在进入
onClick
时立即实例化所有这些对象,而是在每个switch语句的主体中进行实例化。对于“添加”按钮,只需复制并粘贴即可。对于删除行,将对象设置为要删除的行


您似乎没有办法选择要删除的行,但假设您在其他地方这样做,您可以使用
TableLayout.removeViewAt(int index)
选择哪一行。还有
TableLayout.removeView(视图视图)
如果你有视图本身而不是索引。

先生,你是个天才。非常感谢。我现在唯一的问题是,在我添加一行之后,删除该行,然后再次尝试添加一行,我在第57行收到一个NullPointerException。它将异常指向“testext1.setLayoutParams(editText1.getLayoutParams());”这很奇怪,是否可以将所有LogCat错误编辑到您的问题中?所以我可以看到发生了什么。
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;
    }
}