0

我正在开发应用程序,我需要在其中显示带有进度条的进度对话框,直到从网站上完全下载数据。但是当我运行我的代码时,当整个数据已经从网站完全下载时,它会显示进度对话框,并且在加载它时会在模拟器上显示黑屏。

所以如果用户的互联网连接很慢,我想显示下载过程。

请帮忙。

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();

    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

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




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






    @Override
    protected void onPostExecute(Object result) {
        if(progressDialog != null && progressDialog.isShowing()){
            progressDialog.dismiss ( ) ;
        }
    }
    @Override
    protected Object doInBackground(Void... params) {
        // TODO Auto-generated method stub
           while (progress_status<100)
             {

                 progress_status+=2;
                 setProgress(progress_status);
                 try {
                     Thread.sleep(300);
                 } catch (InterruptedException e) {
                     e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                 }
             }

        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]+"%");


    }






    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
         progressDialog.show ( ) ;
        super.onPreExecute();
    }



}

}
4

3 回答 3

0

onPreExecute在方法上输入代码

if (!progressDialog.isShowing()){
    progressDialog = ProgressDialog.show(AbstractActivity.this, "",getString(R.string.please_wait), false);
}

在中输入代码onPostExecute

if (progressDialog.isShowing())
      progressDialog.dismiss();

你试图展示ProgressDialog之前super.onPreExecute();

尝试后super.onPreExecute();

@Override
protected void onPreExecute() {
    super.onPreExecute();
    progressDialog.show ( ) ;
}
于 2013-03-28T09:51:51.090 回答
0

请尝试以下方法:

@Override
    protected void onPreExecute() {
        progressDialog.show () ;
    }

我遇到了类似的问题,我将其更改为上述问题并解决了。

于 2013-03-28T09:55:22.547 回答
-1
progressDialog.create();
progressDialog.show();

这行你忘了,写在下面

progressDialog.setTitle ( "Please wait" ) ;
progressDialog.setIndeterminate ( true ) ;
于 2013-03-28T09:45:20.390 回答