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 を完全に表示しますが、進行状況ダイアログの後、空白の画面が表示されます。この問題を解決するのを手伝ってください。

ありがとう

4

3 に答える 3

2
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();
         }
}
于 2011-09-21T09:17:39.910 に答える
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();

        }   
    }
}

これをメインで使用します::

new xyz().execute();
于 2011-09-21T07:37:54.867 に答える
1

関数を追加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();
    }
}
于 2011-09-21T07:50:46.863 に答える