2012-02-23 51 views
8

Tôi đang tạo một ứng dụng Android và nó phải tải một số dữ liệu mặc dù Internet (chỉ một số dữ liệu-- không phải tất cả). Vì vậy, khi dữ liệu đang tải và kết nối Internet chậm, tôi muốn hiển thị biểu tượng "Đang tải ..." cho người dùng.Cách hiển thị trạng thái "Đang tải" trong Android?

Vậy làm cách nào tôi có thể thực hiện việc này? Hiển thị biểu tượng "Đang tải ..." trong khi dữ liệu đang được tải trong nền và khi dữ liệu được tải hoàn toàn, hãy ẩn biểu tượng?

Cảm ơn trước!

+0

thấy điều này là sử dụng 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

Trả lời

21

sử dụng Công việc không đồng bộ cho trạng thái của bạn.

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

Cảm ơn rất nhiều vì câu trả lời! Nó thực sự mang tính thông tin và hữu ích! – Roshnal

+0

@Roshnal hoặc xem liên kết này cũng http://android-developers.blogspot.in/2010/07/multithreading-for-performance.html –

2

Sử dụng AsyncTask cùng với hộp thoại tiến trình khi hoàn thành tác vụ..Điều này sẽ làm ..

0

Trong phương thức 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

Sử dụng AsyncTask cho các hoạt động nền, sau đó hiển thị hộp thoại tiến triển như dưới đây

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; 
     } 
     } 


} 

}

Các vấn đề liên quan