Android:ProgressDialog 和显示 XML 解析问题的数据

发布于 2024-12-05 19:27:52 字数 1976 浏览 0 评论 0原文

我正在从 URL 解析 XML 并显示它。我想在我的 XML 解析器方法解析 XML 并准备好显示时显示 ProgressDialog。我面临的问题是 ProgressDialog 之后没有显示任何数据,而是显示空白屏幕。

这是我的代码:

ArrayList<XMLItem> items= new ArrayList<XMLItem>();
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.mydata);
    adapter= new MyAdapter(this, R.layout.customcell, items);
    listView= (ListView) findViewById(id.ListView01);
    listView.setAdapter(adapter);

    final ProgressDialog dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);

    Thread thread=new Thread(new Runnable(){

        public void run(){

            doTheAutoRefresh();
            runOnUiThread(new Runnable(){

                public void run() {
                    if(dialog.isShowing())
                        dialog.dismiss();
                            adapter.notifyDataSetChanged(); //this line throws an exception and if i comment this line, blank screen is shown

                }

            });
        }

        });
        thread.start(); 
}

private void doTheAutoRefresh() {
    handler.postDelayed(new Runnable() {
        public void run(){
            loadData(); // this is where you put your refresh code
            doTheAutoRefresh();
             }
         }, 15000);

}

private void loadData(){
    Document doc;
    try {
        doc = XMLReader.parseURL("my url to parse xml");



    NodeList nl = doc.getElementsByTagName("l");



    for(int i=0; i<nl.getLength(); i++){

        NamedNodeMap np = nl.item(i).getAttributes();

        String t= np.getNamedItem("htn").getNodeValue();
        String sT= np.getNamedItem("atn").getNodeValue();
        XMLItem item= new XMLItem(t, sT);
        items.add(item);

    }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

此代码完美地显示了 ProgressDialog,但在进度对话框之后,它显示空白屏幕。请帮我解决这个问题。

谢谢

I am parsing an XML from a URL and showing it. I want to show ProgressDialog while my XML Parser Method parse the XML and get it ready to show. The problem I am facing is that no data is shown after ProgressDialog rather it shows blank screen.

Here is my code:

ArrayList<XMLItem> items= new ArrayList<XMLItem>();
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.mydata);
    adapter= new MyAdapter(this, R.layout.customcell, items);
    listView= (ListView) findViewById(id.ListView01);
    listView.setAdapter(adapter);

    final ProgressDialog dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);

    Thread thread=new Thread(new Runnable(){

        public void run(){

            doTheAutoRefresh();
            runOnUiThread(new Runnable(){

                public void run() {
                    if(dialog.isShowing())
                        dialog.dismiss();
                            adapter.notifyDataSetChanged(); //this line throws an exception and if i comment this line, blank screen is shown

                }

            });
        }

        });
        thread.start(); 
}

private void doTheAutoRefresh() {
    handler.postDelayed(new Runnable() {
        public void run(){
            loadData(); // this is where you put your refresh code
            doTheAutoRefresh();
             }
         }, 15000);

}

private void loadData(){
    Document doc;
    try {
        doc = XMLReader.parseURL("my url to parse xml");



    NodeList nl = doc.getElementsByTagName("l");



    for(int i=0; i<nl.getLength(); i++){

        NamedNodeMap np = nl.item(i).getAttributes();

        String t= np.getNamedItem("htn").getNodeValue();
        String sT= np.getNamedItem("atn").getNodeValue();
        XMLItem item= new XMLItem(t, sT);
        items.add(item);

    }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

This code shows ProgressDialog perfectly, but after progress dialog, it shows blank screen. Please help me sorting out this problem.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浮萍、无处依 2024-12-12 19:27:52
private class xyz extends AsyncTask<Void, Void, Void> {
    private final ProgressDialog dialog = new ProgressDialog(tranning.this);

    @Override
    protected void onPreExecute() {
        this.dialog.setMessage("Please Wait...");
        this.dialog.show();  
    }


    @Override
    protected Void doInBackground(Void... arg0) {
        // do your load data function part here.
        // just populate the data structures that are necessary 
        // for the adapter.
        return null;
    }

    @Override
    protected void onPostExecute(final Void unused) {
        if (this.dialog.isShowing()) {
          this.dialog.dismiss();
          // notify adapter here.
          adapter.notifyDataSetChanged();
          refreshTimer(); // to do the same thing after 15 seconds.
        }   
    }
}

现在可能在 onCreate() 中第一次调用 asycTask 。

new xyz().execute()

创建一个函数来设置刷新计时器,我们通过使用 postDelayed 来做到这一点。

private void refreshTimer(){
       handler.postDelayed( runner , 15000);
}

private Runnable runner  = new Runnable(){
         public void run(){
               new xyz().execute();
         }
}
private class xyz extends AsyncTask<Void, Void, Void> {
    private final ProgressDialog dialog = new ProgressDialog(tranning.this);

    @Override
    protected void onPreExecute() {
        this.dialog.setMessage("Please Wait...");
        this.dialog.show();  
    }


    @Override
    protected Void doInBackground(Void... arg0) {
        // do your load data function part here.
        // just populate the data structures that are necessary 
        // for the adapter.
        return null;
    }

    @Override
    protected void onPostExecute(final Void unused) {
        if (this.dialog.isShowing()) {
          this.dialog.dismiss();
          // notify adapter here.
          adapter.notifyDataSetChanged();
          refreshTimer(); // to do the same thing after 15 seconds.
        }   
    }
}

now call the asycTask for the first time in your onCreate() probably.

new xyz().execute()

make a function to put a timer for refresh, we do that by using postDelayed.

private void refreshTimer(){
       handler.postDelayed( runner , 15000);
}

private Runnable runner  = new Runnable(){
         public void run(){
               new xyz().execute();
         }
}
偏闹i 2024-12-12 19:27:52

我认为你需要实现 AsyncTask ::

private class xyz extends AsyncTask<Void, Void, Void> {
    private final ProgressDialog dialog = new ProgressDialog(tranning.this);

    @Override
    protected void onPreExecute() {
        this.dialog.setMessage("Please Wait...");
        this.dialog.show();
        // put your code which preload with processDialog  
    }


    @Override
    protected Void doInBackground(Void... arg0) {
        // put your code here
        return null;
    }

    @Override
    protected void onPostExecute(final Void unused) {
        if (this.dialog.isShowing()) {
          this.dialog.dismiss();

        }   
    }
}

并在 main :: 中使用它

new xyz().execute();

i think you need to implement AsyncTask ::

private class xyz extends AsyncTask<Void, Void, Void> {
    private final ProgressDialog dialog = new ProgressDialog(tranning.this);

    @Override
    protected void onPreExecute() {
        this.dialog.setMessage("Please Wait...");
        this.dialog.show();
        // put your code which preload with processDialog  
    }


    @Override
    protected Void doInBackground(Void... arg0) {
        // put your code here
        return null;
    }

    @Override
    protected void onPostExecute(final Void unused) {
        if (this.dialog.isShowing()) {
          this.dialog.dismiss();

        }   
    }
}

and use this in main ::

new xyz().execute();
失眠症患者 2024-12-12 19:27:52

添加一个函数 showData() 并在关闭对话框后调用它

ArrayList<XMLItem> items= new ArrayList<XMLItem>();
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.mydata);

    listView= (ListView) findViewById(id.ListView01);

    final ProgressDialog dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);

    Thread thread=new Thread(new Runnable(){

        public void run(){

            loadData();
            runOnUiThread(new Runnable(){

                public void run() {
                    if(dialog.isShowing()){
                        dialog.dismiss();
                        showData(); //////////////////////////////////////
                    }

                }

            });
        }

        });
        thread.start(); 
}
private void showData(){
    adapter= new MyAdapter(this, R.layout.customcell, items);
    listView.setAdapter(adapter);
}
private void loadData(){
    Document doc;
    try {
        doc = XMLReader.parseURL("my url to parse xml");



    NodeList nl = doc.getElementsByTagName("l");



    for(int i=0; i<nl.getLength(); i++){

        NamedNodeMap np = nl.item(i).getAttributes();

        String t= np.getNamedItem("htn").getNodeValue();
        String sT= np.getNamedItem("atn").getNodeValue();
        XMLItem item= new XMLItem(t, sT);
        items.add(item);

    }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

add a function showData() and call it after dismissing the dialog

ArrayList<XMLItem> items= new ArrayList<XMLItem>();
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.mydata);

    listView= (ListView) findViewById(id.ListView01);

    final ProgressDialog dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);

    Thread thread=new Thread(new Runnable(){

        public void run(){

            loadData();
            runOnUiThread(new Runnable(){

                public void run() {
                    if(dialog.isShowing()){
                        dialog.dismiss();
                        showData(); //////////////////////////////////////
                    }

                }

            });
        }

        });
        thread.start(); 
}
private void showData(){
    adapter= new MyAdapter(this, R.layout.customcell, items);
    listView.setAdapter(adapter);
}
private void loadData(){
    Document doc;
    try {
        doc = XMLReader.parseURL("my url to parse xml");



    NodeList nl = doc.getElementsByTagName("l");



    for(int i=0; i<nl.getLength(); i++){

        NamedNodeMap np = nl.item(i).getAttributes();

        String t= np.getNamedItem("htn").getNodeValue();
        String sT= np.getNamedItem("atn").getNodeValue();
        XMLItem item= new XMLItem(t, sT);
        items.add(item);

    }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文