2013-03-27 35 views
2

我遇到了一個問題,我有一個方法,我稱之爲processInfo()。該方法基本上處理從NFC標籤或QR碼收集的信息並將其寫入數據庫。 根據某些情況,processInfo方法處理如何將信息寫入數據庫。 processInfo方法返回一個布爾值,如果設置爲true,則DB中的信息將被髮送到Web服務器。AlertDialog框結果之前返回的方法

我在processInfo中有一些邏輯,說如果條件A然後寫入數據庫並返回true。這反過來將它發送到wewbservice。如果條件B然後顯示一個Alertdialog框。 Alertdialog框有一個OK和CANCEL按鈕。如果按下OK,則執行條件A中的操作,如果CANCEL被按下,則解除對話框並返回false。

發生的情況是,如果條件B發生,對話框將按預期顯示,但在任何按鈕被按下之前它將返回調用方法。我怎樣才能讓應用程序掛起,直到至少有一個按鈕被按下?

我試過使用while(! alertDialog.isShowing == true) -> return boolean。但在alertDialog.show()後退出,回到調用方法。

success = false; 

if(condition A) { 
    // write to DB and return to caller 
    return success = true; 
} else { 
    success = false; 

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(NfcscannerActivity.this); 
    // set title 
    alertDialogBuilder.setTitle("Please logout after "); 
    // set dialog message 
    alertDialogBuilder.setMessage("Click Ok to return to the menu") 
    .setCancelable(false) 
    .setPositiveButton("Ok",new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog,int id) { 
      success = true; 
     // write to DB           
     } 
    }) 
    .setNegativeButton("Cancel",new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog,int id) { 
      // if this button is clicked, just close 
      // the dialog box and do nothing 
      success = false; 
      dialog.cancel(); 

     } 
    }); 

    // create alert dialog 
    AlertDialog alertDialog = alertDialogBuilder.create(); 

    // show it 
    alertDialog.show(); 
} 

return success; 
} 
+0

@Nitish嗨,不幸的是我之前試過,但我使用匿名內部類DialogInterface.onClickListener和壓倒一切其onClick有一個返回類型的void。所以你不能返回布爾型 – turtleboy 2013-03-27 13:30:58

+0

你意識到我的錯誤,這就是爲什麼我刪除了我的文章。 – Nitish 2013-03-27 13:33:17

回答

1

@turtleboy這是一個簡單的例子

公共類測試活動延伸活動{

static final String TAG="TestActivity"; 

    static final int MSG_SEND_INFO_TO_SERVER = 1; 

    static final int MSG_INFO_NOT_SEND_TO_SERVER = 2; 

    boolean condition=false; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     Log.i(TAG, "0000000000000000"); 
     your_methode(); 

    } 

    Handler communication_handler =new Handler() { 
     @Override 
     public void handleMessage(Message msg) { 
      switch (msg.what) { 

      case MSG_SEND_INFO_TO_SERVER: 
       //send info to the web server 
       Log.i(TAG, "11111111111111111"); 
       Toast.makeText(getApplicationContext(), "send msg to the server", Toast.LENGTH_SHORT).show(); 

       break; 
      case MSG_INFO_NOT_SEND_TO_SERVER: 
       //send info to the web server 
       Log.i(TAG, "222222222222222222"); 
       Toast.makeText(getApplicationContext(), "no msg to send to the server", Toast.LENGTH_SHORT).show(); 

       break; 
      default: 
       break; 
      } 
     } 
    }; 

    public void your_methode() 
    { 
     if(condition) { 
      // write to DB 
      communication_handler.sendMessage(Message.obtain(null,MSG_INFO_NOT_SEND_TO_SERVER,0)); 
      return ; 
     } else { 
      AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); 
      // set title 
      alertDialogBuilder.setTitle("Please logout after "); 
      // set dialog message 
      alertDialogBuilder.setMessage("Click Ok to return to the menu").setCancelable(false); 
      alertDialogBuilder.setPositiveButton("Ok",new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog,int id) { 
        // write to DB 
        // send msg 
        communication_handler.sendMessage(Message.obtain(null,MSG_SEND_INFO_TO_SERVER,0));          
       } 
      }); 
      alertDialogBuilder.setNegativeButton("Cancel",new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog,int id) { 
        // if this button is clicked, just close 
        // the dialog box and do nothing 
        dialog.cancel(); 

        communication_handler.sendMessage(Message.obtain(null,MSG_INFO_NOT_SEND_TO_SERVER,0)); 

       } 
      }); 

      // create alert dialog 
      AlertDialog alertDialog = alertDialogBuilder.create(); 

      // show it 
      alertDialog.show(); 
     } 
    } 
} 
+0

嘿,非常感謝代碼!它效果很好。 – turtleboy 2013-03-28 12:02:47

2

您可以使用回調方法。比方說,onResultObtained(boolean result)

boolean onResultObtained(boolean result) { 
    if(result) { 
     //write to DB and return to caller 
     return true; 
    } else { 
     return false; 
    } 
} 

實際代碼

if(condition A){ 

    onResultChanged(true); 

}else{ 
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
           NfcscannerActivity.this); 

          // set title 
          alertDialogBuilder.setTitle("Please logout after "); 

          // set dialog message 
          alertDialogBuilder 
           .setMessage("Click Ok to return to the menu") 
           .setCancelable(false) 
           .setPositiveButton("Ok",new DialogInterface.OnClickListener() { 
            public void onClick(DialogInterface dialog,int id) { 
           onResultChanged(true); 


            } 
            }) 
           .setNegativeButton("Cancel",new DialogInterface.OnClickListener() { 
            public void onClick(DialogInterface dialog,int id) { 
             // if this button is clicked, just close 
             // the dialog box and do nothing 

             dialog.dismiss(); 
             onResultChanged(false); 
            } 
           }); 

           alertDialogBuilder.show(); 


} 
} 

另一種方式來做到這一點,是讓不同程度的成功作爲全球。

boolean success; 

processInfo()

private void processInfo() { 
    if (condition A) { 
     success = true; 
     //Save data to DB 
    } else { 
     AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
       MainActivity.this); 

     // set title 
     alertDialogBuilder.setTitle("Please logout after "); 

     // set dialog message 
     alertDialogBuilder 
       .setMessage("Click Ok to return to the menu") 
       .setCancelable(false) 
       .setPositiveButton("Ok", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, 
            int id) { 
           success = true; 
           //Save data to DB 
          } 
         }) 
       .setNegativeButton("Cancel", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, 
            int id) { 
           // if this button is clicked, just close 
           // the dialog box and do nothing 
           dialog.dismiss(); 
           success = false; 
          } 
         }); 

     alertDialogBuilder.show(); 

    } 
} 

而且,您的電話()

private void callingMethod(){ 
    if(success){ 
     //do your stuff 
    } else { 
     //do your stuff 
    } 
} 
+0

不幸的是,沒有工作。謝謝 – turtleboy 2013-03-27 14:11:54

+0

@turtleboy什麼沒有工作呢?如果你需要幫助,你會有一個更具體的, – FoamyGuy 2013-03-27 15:05:36

+0

@FoamyGuy嗨,它沒有工作,因爲程序繼續執行,一旦對話框出現。該對話框是在名爲processInfo()的方法內部創建的。這個方法返回一個布爾值。調用processInfo測試返回true或false的應用程序部分會相應地執行。所以如果返回true,那麼數據庫中的數據將被髮送到webservice。在用戶點擊確定/取消鍵之前,用此代碼返回。所以Web服務永遠不會被調用。希望有所幫助。謝謝 – turtleboy 2013-03-27 15:48:11

1

有Android中沒有阻塞UI模型,一切都asynchronous.You必須改變行爲。 您可以調用線程或asyncTast或looperThread中的alertDialog,並且如果數據庫中的信息必須發送到Web服務器,則將消息發送到主線程(使用Handler)並在handleMessage(消息消息)中發送信息。

+0

嗨,你能擴展一點簡單的例子嗎? – turtleboy 2013-03-27 14:13:18

相關問題