2012-02-23 31 views
8

我正在製作一個Android應用程序,它必須通過Internet加載一些數據(只有一些數據 - 不是全部)。所以當數據加載時,互聯網連接速度較慢時,我想向用戶顯示「加載...」圖標。如何在Android中顯示「加載」狀態?

那麼我該如何做到這一點?當數據在後臺加載時顯示「正在加載...」圖標,當完全加載時,隱藏圖標?

在此先感謝!

+0

看到這是使用FUL [HTTP: //www.41post.com/4588/programming/android-coding-a-loading-screen-part-1](http://www.41post.com/4588/programming/android-coding-a-loading-scree n-part-1) – NagarjunaReddy 2012-02-23 05:57:37

回答

21

使用異步任務的狀態一起。

new SomeTask(0).execute(); 

/** Inner class for implementing progress bar before fetching data **/ 
private class SomeTask extends AsyncTask<Void, Void, Integer> 
{ 
    private ProgressDialog Dialog = new ProgressDialog(yourActivityClass.this); 

    @Override 
    protected void onPreExecute() 
    { 
     Dialog.setMessage("Doing something..."); 
     Dialog.show(); 
    } 

    @Override 
    protected Integer doInBackground(Void... params) 
    { 
     //Task for doing something 

     return 0; 
    } 

    @Override 
    protected void onPostExecute(Integer result) 
    { 

     if(result==0) 
     { 
      //do some thing 
     } 
     // after completed finished the progressbar 
     Dialog.dismiss(); 
    } 
} 
+0

非常感謝您的回答!這真的很有幫助! – Roshnal 2012-02-23 07:12:10

+0

@Roshnal乾草檢查此鏈接也http://android-developers.blogspot.in/2010/07/multithreading-for-performance.html – 2012-02-23 07:24:45

2

使用的AsyncTask與進度對話框上的任務completion..That會做..

0

在onCreate方法:

WebView mWebView; 
ProgressDialog pgDiagWebView; 

@Override 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.webview); 
    pgDiagWebView = ProgressDialog.show(CreateAccountWebView.this, "Loading", "Wait", true); 
    mWebView = (WebView) findViewById(R.id.registerWebView); 
    mWebView.getSettings().setJavaScriptEnabled(true); 
    mWebView.setWebViewClient(new ResgisterWebViewClient()); 
    mWebView.loadUrl("http://www.google.com/"); 
} 

class ResgisterWebViewClient extends WebViewClient { 
    @Override 
    public void onPageFinished(WebView view, String url) { 
     // TODO Auto-generated method stub 
     super.onPageFinished(view, url); 
     pgDiagWebView.dismiss(); 
    } 
} 
1

用於後臺操作中使用的AsyncTask,然後顯示進度對話框像下面

private class ProgressTask extends AsyncTask<String, Void, Boolean> { 
    private ProgressDialog dialog; 
    List<Message> titles; 
    private ListActivity activity; 
    //private List<Message> messages; 
    public ProgressTask(ListActivity activity) { 
     this.activity = activity; 
     context = activity; 
     dialog = new ProgressDialog(context); 
    } 



    /** progress dialog to show user that the backup is processing. */ 

    /** application context. */ 
    private Context context; 

    protected void onPreExecute() { 
     this.dialog.setMessage("Progress start"); 
     this.dialog.show(); 
    } 

     @Override 
    protected void onPostExecute(final Boolean success) { 
      List<Message> titles = new ArrayList<Message>(messages.size()); 
      for (Message msg : messages){ 
       titles.add(msg); 
      } 
      MessageListAdapter adapter = new MessageListAdapter(activity, titles); 
      activity.setListAdapter(adapter); 
      adapter.notifyDataSetChanged(); 

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

     if (success) { 
      Toast.makeText(context, "OK", Toast.LENGTH_LONG).show(); 
     } else { 
      Toast.makeText(context, "Error", Toast.LENGTH_LONG).show(); 
     } 
    } 

    protected Boolean doInBackground(final String... args) { 
     try{  
      BaseFeedParser parser = new BaseFeedParser(); 
      messages = parser.parse(); 


      return true; 
     } catch (Exception e){ 
      Log.e("tag", "error", e); 
      return false; 
     } 
     } 


} 

}

相關問題