进度对话框不加载数据的时间显示

进度对话框不加载数据的时间显示

本文介绍了进度对话框不加载数据的时间显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的应用程序中,我需要显示有进度条进度对话框,直到数据完全从网站上下载。
但是,当我运行我的code将其显示进度对话框,当整个数据已经完全从网站上下载,并在装载时它显示我在模拟器黑屏。

所以我想说明下载过程中,如果用户的互联网连接速度很慢。

请帮忙。

 包com.androidhive.xmlparsing;进口的java.util.ArrayList;
进口的java.util.HashMap;进口org.w3c.dom.Document中;
进口org.w3c.dom.Element中;
进口org.w3c.dom.NodeList;进口android.app.ListActivity;
进口android.app.ProgressDialog;
进口android.content.Context;
进口android.content.DialogInterface;
进口android.content.DialogInterface.OnCancelListener;
进口android.content.Intent;
进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.AdapterView;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.ListAdapter;
进口android.widget.ListView;
进口android.widget.ProgressBar;
进口android.widget.SimpleAdapter;
进口android.widget.TextView;公共类AndroidXMLParsingActivity扩展ListActivity {
TextView中的TextView;
ArrayList的<&HashMap的LT;字符串,字符串>>菜单项;
//所有静态变量
静态最终字符串URL =htt​​p://feeds.bbci.co.uk/news/rss.xml;
// XML节点键
静态最后弦乐KEY_ITEM =项; //父节点
静态最后弦乐KEY_ID =ID;
静态最后弦乐KEY_NAME =称号;
//静态最后弦乐KEY_COST =成本;
静态最后弦乐KEY_DESC =说明;
静态最后弦乐KEY_IMAGE =媒体:缩略图@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);    新HeavyWorker(AndroidXMLParsingActivity.this).execute();    菜单项=新的ArrayList<&HashMap的LT;字符串,字符串>>();    XMLParser的解析器=新XMLParser的();
    字符串XML = parser.getXmlFromUrl(URL); //获取XML
    文档的文档= parser.getDomElement(XML); //获得DOM元素    节点列表NL = doc.getElementsByTagName(KEY_ITEM);
    项目> //遍历所有项目节点&LT循环;
    的for(int i = 0; I< nl.getLength();我++){
        //创建新的HashMap
        HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();
        元素e =(元)nl.item(I)
        //将每个子节点的HashMap键=>值
        map.put(KEY_ID,parser.getValue(即KEY_ID));
        map.put(KEY_NAME,parser.getValue(即KEY_NAME));
    // map.put(KEY_COST,RS+ parser.getValue(即KEY_COST));
        map.put(KEY_DESC,parser.getValue(即KEY_DESC));
        map.put(KEY_IMAGE,parser.getValue(即KEY_IMAGE));        //添加HashList到ArrayList的
        menuItems.add(地图);
    }
    //添加菜单项到ListView控件
    ListAdapter适配器=新SimpleAdapter(这一点,菜单项,
            R.layout.list_item,
            新的String [] {KEY_NAME,KEY_DESC,KEY_IMAGE},新的INT [] {
                    R.id.name,R.id.desciption,R.id.image});    setListAdapter(适配器);    //选择单个ListView项
    ListView控件LV = getListView();    lv.setOnItemClickListener(新OnItemClickListener(){        @覆盖
        公共无效onItemClick(适配器视图<>母公司,观景,
                INT位置,长的id){
            //从选定的ListItem得到的值
            字符串名称=((的TextView)view.findViewById(R.id.name))的getText()的toString()。
        //字符串成本=((的TextView)view.findViewById(R.id.cost))的getText()的toString()。
            字符串描述=((的TextView)view.findViewById(R.id.desciption))的getText()的toString()。
            //开始新意图
            在意向=新意图(getApplicationContext(),SingleMenuItemActivity.class);
            in.putExtra(KEY_NAME,名);
            //in.putExtra(KEY_COST,成本);
            in.putExtra(KEY_DESC,说明);
            startActivity(在);        }
    });/ *}
    })。开始();     * /
         }公共类HeavyWorker扩展的AsyncTask<太虚,整数,对象> {    进度进度=(进度)findViewById(R.id.progress);    INT progress_status;    私人ProgressDialog progressDialog;
    私人语境targetCtx;    公共HeavyWorker(上下文的背景下){
        this.targetCtx =背景;
       // this.needToShow = TRUE;
        progressDialog =新ProgressDialog(targetCtx);
        progressDialog.setCancelable(假);
        progressDialog.setMessage(检索数据...);
        progressDialog.setTitle(请稍候);
        progressDialog.setIndeterminate(真);
    }
    @覆盖
    保护无效onPostExecute(对象结果){
        如果(progressDialog =空&放大器;!&放大器; progressDialog.isShowing()){
            progressDialog.dismiss();
        }
    }
    @覆盖
    保护对象doInBackground(虚空...... PARAMS){
        // TODO自动生成方法存根
           而(progress_status< 100)
             {                 progress_status + = 2;
                 setProgress(progress_status);
                 尝试{
                     视频下载(300);
                 }赶上(InterruptedException的E){
                     e.printStackTrace(); //改变catch语句使用文件的身体|设置|文件模板。
                 }
             }        返回null;
    }
    @覆盖
    保护无效onProgressUpdate(整数...值){
        // TODO自动生成方法存根
        super.onProgressUpdate(值);
        progressBar.setProgress(值[0]);
             textView.setText(下载+值[0] +%);
    }
    @覆盖
    在preExecute保护无效(){
        // TODO自动生成方法存根
         progressDialog.show();
        super.on preExecute();
    }}}


解决方案

请尝试以下操作:

  @覆盖
    在preExecute保护无效(){
        progressDialog.show();
    }

我有一个类似的问题,我改成了上述和问题得到解决。

I am working on application in which I need to show progress dialog with progress bar until data is completely downloaded from the website.But when I run my code it shows progress dialog when whole data is already completely download from website and at the time of loading it show me black screen on emulator.

so I want to show downloading process if user's internet connection is slow.

please help.

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



}

}
解决方案

Please try the following:

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

I was having a similar problem and i changed it to the above and problem was solved.

这篇关于进度对话框不加载数据的时间显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 09:18