[英]Manage display timing of progress dialog in android
問題是,如果用戶的互聯網連接速度較慢,那么我想在從網站加載數據的過程中顯示進度對話框。 我使用AsyncTask類來實現這一點。 但是,當我運行我的代碼時,當數據已經加載到屏幕上時,將顯示對話框。 在加載期間,我在模擬器上看到黑屏。 請提供建議,以顯示如何在加載開始時顯示“進度”對話框,並在加載結束時自動將其關閉。 我不想在啟動時看到黑屏。 這是我的主要Java文件。 請幫忙..................
package com.androidhive.xmlparsing;
import java.util.ArrayList;
import java.util.HashMap;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class AndroidXMLParsingActivity extends ListActivity {
TextView textView;
ArrayList<HashMap<String, String>> menuItems;
// All static variables
static final String URL = "http://feeds.bbci.co.uk/news/rss.xml" ;
// XML node keys
static final String KEY_ITEM = "item"; // parent node
static final String KEY_ID = "id";
static final String KEY_NAME = "title";
//static final String KEY_COST = "cost";
static final String KEY_DESC = "description";
static final String KEY_IMAGE = "media:thumbnail";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new HeavyWorker(AndroidXMLParsingActivity.this).execute();
System.out.println("you are on print line 1");
menuItems = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
System.out.println("you are on print line 2");
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
// map.put(KEY_COST, "Rs" + parser.getValue(e, KEY_COST));
map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
map.put(KEY_IMAGE, parser.getValue(e, KEY_IMAGE));
// adding HashList to ArrayList
menuItems.add(map);
}
System.out.println("you are on print line 3");
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(this, menuItems,
R.layout.list_item,
new String[] { KEY_NAME, KEY_DESC, KEY_IMAGE}, new int[] {
R.id.name, R.id.desciption, R.id.image });
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
// String cost = ((TextView) view.findViewById(R.id.cost)).getText().toString();
String description = ((TextView) view.findViewById(R.id.desciption)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(KEY_NAME, name);
//in.putExtra(KEY_COST, cost);
in.putExtra(KEY_DESC, description);
startActivity(in);
}
});
/* }
}).start();
*/
}
public class HeavyWorker extends AsyncTask < Void , Integer, Object > {
ProgressBar progressBar = (ProgressBar)findViewById(R.id.progress);
int progress_status;
private ProgressDialog progressDialog ;
private Context targetCtx ;
public HeavyWorker ( Context context ) {
this.targetCtx = context ;
// this.needToShow = true;
progressDialog = new ProgressDialog ( targetCtx ) ;
progressDialog.setCancelable ( false ) ;
progressDialog.setMessage ( "Retrieving data..." ) ;
progressDialog.setTitle ( "Please wait" ) ;
progressDialog.setIndeterminate ( true ) ;
System.out.println("you are on print line 5");
}
@Override
protected void onPostExecute(Object result) {
if(progressDialog != null && progressDialog.isShowing()){
progressDialog.dismiss ( ) ;
}
System.out.println("you are on print line 6");
}
@Override
protected Object doInBackground(Void... params) {
// TODO Auto-generated method stub
while (progress_status<100)
{
progress_status+=2;
setProgress(progress_status);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
System.out.println("you are on print line 7");
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
progressBar.setProgress(values[0]);
textView.setText("downloading " +values[0]+"%");
System.out.println("you are on print line 8");
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
progressDialog.show ( ) ;
super.onPreExecute();
System.out.println("you are on print line 9");
}
}
}
加載大型數據表單服務器時,您必須管理后台線程。 看我會盡力向你解釋。
請參閱此處示例鏈接
在線程啟動之前,調用1個完成任務。
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
// This will call before the loading the URL data. so if you want to get data from locally that logic should be written here.
}
}
在后台通過紅色 @Override受保護的對象doInBackground(Void ... params) 調用2個加載數據 {// TODO自動生成的方法存根
while (progress_status<100)
{
progress_status+=2;
setProgress(progress_status);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
System.out.println("you are on print line 7");
return null;
}
調用3,這是UI線程,它將根據重試數據更新UI。
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
// show your dialog here progressDialog.show ( ) ;
super.onProgressUpdate(values);
progressBar.setProgress(values[0]);
textView.setText("downloading " +values[0]+"%");
System.out.println("you are on print line 8");
}
調用線程4的結尾。 因此顯示最終輸出或釋放控制。
@Override
protected void onPostExecute(Object result) {
if(progressDialog != null && progressDialog.isShowing()){
progressDialog.dismiss ( ) ;
}
System.out.println("you are on print line 6");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.