2012-07-21 46 views
0

似乎這個主題有幾個變化。我向用戶顯示AlertBox是否想要保存此項目?如果他們反應良好,那麼我想讓Alertbox消失以替換爲ProgressDialog框,然後當物品完成保存時它將被解散。Android AlertBox然後ProgressDialog。 ProgessDialog沒有顯示

下面的當前代碼顯示Ok/Cancel AlertBox並正確關閉並顯示正確的烤麪包。但是如果用戶選擇OK,ProgressDialog顯示畢竟完成並永不消失。確定/取消按鈕保持按下狀態,直到項目被保存。如果用戶按OK,我想讓AlertBox消失,然後顯示ProgressDialog,並在完成保存時關閉它。

{ 
Vibrate(ClickVibrate); 
final ProgressDialog Dialog = ProgressDialog.show(v.getRootView().getContext(), "Loading", "Please wait...", true); 
if(AlertDialogProcessing==0) 
{  
ProgressDialog progress; 
final String title="Save Item"; 
final String message="Press OK to save or CANCEL."; 
final String ok="OK"; 
final String cancel="CANCEL"; 

final AlertDialog.Builder alertbox = new AlertDialog.Builder(this); 
alertbox.setCancelable(true); 
alertbox.setIcon(android.R.drawable.ic_dialog_alert); 
alertbox.setTitle(title); 
alertbox.setMessage(message); 
alertbox.setNegativeButton(cancel, null); 

final AlertDialog dlg = alertbox.create(); 

alertbox.setPositiveButton(ok,new DialogInterface.OnClickListener() 
    { 
    public void onClick(DialogInterface arg0, int arg1) 
    { 
     dlg.dismiss(); 
     Dialog.show(); 
     Vibrate(ClickVibrate); 
     Drawable drawable= getItem(imageSelect); 
     AlertDialogProcessing=1; 
     //task that takes 3 seconds 
     AlertDialogProcessing=0; 
     Toast.makeText(getApplicationContext(), "Item Saved.", Toast.LENGTH_LONG).show(); 
    } 
    }); 
alertbox.setNegativeButton(cancel,new DialogInterface.OnClickListener(){ public void onClick(DialogInterface arg0, int arg1){AlertDialogProcessing=0; Vibrate(ClickVibrate); } }); 
alertbox.show(); 
} 
Dialog.dismiss(); 
} 

回答

1

你調用內部的正面按鈕的onClick()ProgressDialog.show()ProgressDialog.dismiss(),難怪ProgressDialog不顯示。如果保存過程需要一些時間,以至於您需要ProgressDialog,我強烈建議使用AsyncTask類。它在工作線程上運行任務,併爲您提供使用當前任務進度更新UI的可能性。希望這可以幫助。

+0

我還沒有掌握異步。等待做其他事情的時間可以。我應該如何構造代碼。我修改了顯示的代碼以顯示我的內容。 – user1445716 2012-07-21 15:59:15

+0

@ user1445716,請描述您的應用程序的保存過程請 – Egor 2012-07-21 18:57:00

+0

我決定繼續並按照您的建議找出Asynch來執行此操作。涼。我發佈的問題和我最終的答案在這裏[鏈接](http://stackoverflow.com/questions/6494002) – user1445716 2012-07-22 15:13:53