2012-12-17 148 views
0

對話框不出現,並且數據仍然插入到數據庫中,而其空白...關於對話框i的所有代碼我已經使用但仍然可以運行。只想在我的當前頁面出現一個警告框,即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(); 
      } 

      } 
     }); 
+0

http://whathaveyoutried.com? – njzk2

回答

1

爲什麼不使用這種方法?

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 
} 
+1

謝謝!!我做到了...... – user1906140

1

比較字符串使用:

YourString.equals("") 

可以使用

name.equals("") == true 

,而不是

name== null 

在你的if語句

+1

最好使用'「」.equals(yourString)'。如果'YourString'爲'null',你可以得到一個NPE。 –

+0

看到ρяσѕρєяK答案,我剛剛寫了一個例子來向他展示他如何比較字符串,他也可以使用name.equals(null) –

1

但當EditText上不爲空,那麼你顯示警報,從而改變你的代碼,作爲顯示警報時的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 
     } 
    });