【问题标题】:Android: ProgressDialog and Show Data from XML Parsing ProblemAndroid:来自 XML 解析问题的 ProgressDialog 和显示数据
【发布时间】:2011-09-21 07:32:42
【问题描述】:

我正在从 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,但在进度对话框之后,它显示空白屏幕。请帮我解决这个问题。

谢谢

【问题讨论】:

  • 关闭进度对话框后你想做什么,只是在 UI 线程上的代码。在 dialog.dismiss() 之后;它的适配器的 notifyDatasetChanged() 用于您的适配器。我认为您可能希望为此目的使用 AsyncTask。试试看。
  • 什么时候将项目添加到适配器?
  • loaddata完成后你要setAdapter..
  • 您已经设置了适配器,然后更改了数据,所以使用 notifydatasetchanged() 填充数据。
  • 在 dialog.dismiss() 之后尝试在 runonUIThread 上使用 adapter.notifydatasetchanged();看看会发生什么?

标签: android android-progressbar


【解决方案1】:
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();
         }
}

【讨论】:

  • 非常感谢,它解决了我的问题。为你脱帽……标记为正确答案。 :)
【解决方案2】:

我认为你需要实现 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();

【讨论】:

  • 您应该使用它 - 但请注意,对 ListAdapter 内容的所有更改都必须在 onPreExecute、onPostExecute 或 onProgressUpdate 期间的 UI 线程上完成。
【解决方案3】:

添加一个函数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();
    }
}

【讨论】:

  • 我是通过 notifydatasetchanged() 完成的。有用。但我需要每 15 秒更新一次 UI。我写了一个方法 doTheAutoRefresh() 并在线程中调用它。现在,adapter.notifydatasetchanged() 导致异常并强制关闭我的应用程序,如果我评论它,则会显示空白屏幕。我已经编辑了我的帖子。请参阅代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
相关资源
最近更新 更多