0

对话框没有出现,数据仍然插入到数据库中,而它是空的......关于我已经使用但仍然可以运行的对话框的所有代码。只希望在我当前的页面中出现一个警告框,其中edittext为空并需要用户填写它..

if (name == null || inputName.getText().toString().length() == 0)
{  
 if (price == null || inputPrice.getText().toString().length() == 0)
    {  
      if (description == null || inputDesc.getText().toString().length() == 0)
         { 
           // creating new product in background thread
           new CreateNewProduct().execute();
         }
       else
         {
          AlertDialog.Builder alertbox = new  AlertDialog.Builder(NewProductActivity.this)      ;

                       // set the message to display
                       alertbox.setMessage("This is the alertbox!");

            // add a neutral button to the alert box and assign a click listener

             alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() 
            {

               // click listener on the alert box
               public void onClick(DialogInterface arg0, int arg1) {
               // the button was clicked
               Toast.makeText(getApplicationContext(), "OK button clicked", Toast.LENGTH_LONG).show();
             }
             });

                 // show it
                 alertbox.show(); 
       }

       }
        else{
                   AlertDialog.Builder alertbox = new AlertDialog.Builder(NewProductActivity.this);

                   // set the message to display
                   alertbox.setMessage("This is the alertbox!");

                   // add a neutral button to the alert box and assign a click listener
                   alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {

                       // click listener on the alert box
                       public void onClick(DialogInterface arg0, int arg1) {
                           // the button was clicked
                           Toast.makeText(getApplicationContext(), "OK button clicked", Toast.LENGTH_LONG).show();
                       }
                   });

                   // show it
                   alertbox.show(); 
               }


            }else{
                AlertDialog.Builder alertbox = new AlertDialog.Builder(NewProductActivity.this);

                // set the message to display
                alertbox.setMessage("This is the alertbox!");

                // add a neutral button to the alert box and assign a click listener
                alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {

                    // click listener on the alert box
                    public void onClick(DialogInterface arg0, int arg1) {
                        // the button was clicked
                        Toast.makeText(getApplicationContext(), "OK button clicked", Toast.LENGTH_LONG).show();
                    }
                });

                // show it
                alertbox.show(); 
            }

            }
        });
4

3 回答 3

0

你为什么不使用这种方法?

if (name == null || inputName.getText().toString().equals("")
|| price == null || inputPrice.getText().toString().equals("")
|| description == null || inputDesc.getText().toString().equals("")){
       AlertDialog.Builder alertbox = new AlertDialog.Builder(NewProductActivity.this);

                   // set the message to display
                   alertbox.setMessage("This is the alertbox!");

                   // add a neutral button to the alert box and assign a click listener
                   alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {

                       // click listener on the alert box
                       public void onClick(DialogInterface arg0, int arg1) {
                           // the button was clicked
                           Toast.makeText(getApplicationContext(), "OK button clicked", Toast.LENGTH_LONG).show();
                       }
                   });

                   // show it
                   alertbox.show(); 
}
else
{
       //Insert into database
}
于 2012-12-17T12:44:06.723 回答
0

对于比较字符串使用:

YourString.equals("")

您可以使用

 name.equals("") == true

代替

name== null

在你的 if 语句中

于 2012-12-17T12:44:33.710 回答
0

但是当 EditText 不为空时,您将显示 Alert 因此更改代码以在 EditText 为空时显示警报:

if (!name.equals("") || inputName.getText().toString().length() != 0){

               if (!price.equals("") inputPrice.getText().toString().length() != 0)
                {  
                  if (!description.equals("") || inputDesc.getText().toString().length() != 0)
                    { 
                      // creating new product in background thread
                      new CreateNewProduct().execute();
                    }
                   else{
                  // your code here
        }
    });
于 2012-12-17T12:50:34.577 回答