2011-04-12 39 views
0

如何使用AlertDialog將驗證應用於ListView?我的情況是這樣的,我需要讓用戶選擇他們從中進行搜索的狀態。國家的價值在微調中定義。如果用戶在未選擇狀態的情況下點擊Listview項目,我已經在下面定義了一個AlertDialog。具有AlertDialog驗證的ListView

我遇到的問題是警報框顯示,但不會停止執行基礎任務。

在用戶從微調器中選擇狀態之前,如何停止執行我的AsyncTask?是否有任何示例顯示如何驗證用戶是否在啓動AsyncTask之前從微調器中進行了選擇?

list.setOnItemClickListener(new OnItemClickListener() { 

      public void onItemClick(AdapterView<?> parent, View itemClicked, int position, long id) { 

       // This code executes just fine but runs asynchronous with my SearchProducts task. 
       String selectedState = spinner.getSelectedItem().toString(); 
       if(selectedState.equals("Choose Your State")) { 
        alertbox("State", "Please Choose Your State"); 
       }   

       String strText = items[position]; 

       if (strText.equalsIgnoreCase(getResources().getString(R.string.menu_search))) { 
        new SearchProducts().execute(); 

這是我的AlertDialog代碼。

protected void alertbox(String title, String mymessage) { 
     new AlertDialog.Builder(this) 
      .setMessage(mymessage) 
      .setTitle(title) 
      .setCancelable(true) 
      .setNegativeButton(android.R.string.cancel, 
       new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton){} 
       }) 
      .show(); 
     } 
+0

您可以重寫它,以便您顯示對話框或啓動異步任務,而不是顯示對話框並每次啓動異步任務? – James 2011-04-12 16:46:59

回答

0

我覺得自己像這樣一個業餘愛好者,我以正確的方式實現了對話。我的代碼中缺少的是我的onClick for AlertDialog上的「返回」聲明。更新後的代碼如下所示。

protected void alertbox(String title, String mymessage) { 
     new AlertDialog.Builder(this) 
      .setMessage(mymessage) 
      .setTitle(title) 
      .setCancelable(true) 
      .setNegativeButton(android.R.string.cancel, 
       new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton){ 
        return; 
       } 
       }) 
      .show(); 
     }