开发者

Progress bar to show only during long operations

开发者 https://www.devze.com 2023-03-26 17:40 出处:网络
I am designing an application that fetches data from a sqlite database on the click of a button . I want to display a wait message or progress bar during the time of the fetch only if the process is l

I am designing an application that fetches data from a sqlite database on the click of a button . I want to display a wait message or progress bar during the time of the fetch only if the process is long like say more than 3 secs . Otherwise it can just proceed with the program . How do i do this . i tried showing a progress dialog using the following code but it only waits for the specified sleep time and proceeds without showing anything .... plss h开发者_JAVA技巧elp

protected void GetOrders() 
{
    ProgressDialog dialog = null;
    try
    {
        dialog=ProgressDialog.show(loginScreen.this,"PLEASE WAIT","LOADING CONTENTS ..",true);
        //Accesses database 
        allOrders=ProductionOrdersBL.GetOrder();
        Thread.sleep(4000);

    }
    catch(Exception e){}
    finally
    {
        dialog.dismiss(); 
    }
}


first of all for showing progress bar you should use AsyncTask()(this). and then in its preExecute() method say

try{
thread.sleep(3000);
dialog=ProgressDialog.show(loginScreen.this,"PLEASE WAIT","LOADING CONTENTS ..",true);

}catch(){
}

then in postExecute() use

if(dialog.isShowing()){
 dialog.dismiss
}


I found the solution ... I had some operations after the call of the execute method . Thus while the doInBackground() method was executing in a non-UI thread , my UI thread simply continued executing the code that followed the execute method call ... The solution is to perform no task in the main UI thread following the execute method call :)

0

精彩评论

暂无评论...
验证码 换一张
取 消