2012-02-23 25 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; 
     } 
     } 


} 

}

相关问题