加载 XML 文件之前显示进度对话框

发布于 2024-12-11 16:08:11 字数 2866 浏览 0 评论 0原文

我使用以下代码打开 XML 文件并在 ListView 中查看其信息。
我在代码的第一部分中包含了一个 ProgressDialog,以便在文件加载之前显示它,一旦加载,ProgressDialog 就会消失。
问题是当文件已加载时会显示 ProgressDialog。在文件加载之前,屏幕是黑色的,当文件完全加载时,将显示 ListView,同时显示 ProgressDialog。
请帮助我。
谢谢

public class Main extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    final ProgressDialog _progressDialog = new ProgressDialog(this);
    _progressDialog.setIcon(R.drawable.icon);
    _progressDialog.setTitle("Loading ...");
    _progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);

    runOnUiThread(new Runnable() {

        @Override
        public void run() {
            _progressDialog.show();
             Thread t = new Thread() {
                  public void run(){
                      GetData();
                      _progressDialog.dismiss();
                    }
                  };
                  t.run();
        }
    });

}

public boolean GetData() {

    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

    setContentView(R.layout.listplaceholder);

    String xml = XMLfunctions.getXML();
    Document doc = XMLfunctions.XMLfromString(xml);

    int numResults = XMLfunctions.numResults(doc);

    if((numResults <= 0)){
        Toast.makeText(Main.this, "Geen resultaten gevonden", Toast.LENGTH_LONG).show();  
        finish();
    }

    NodeList nodes = doc.getElementsByTagName("result");

    for (int i = 0; i < nodes.getLength(); i++) {                           
        HashMap<String, String> map = new HashMap<String, String>();    

        Element e = (Element)nodes.item(i);
        map.put("id", XMLfunctions.getValue(e, "id"));
        map.put("name", "Naam:" + XMLfunctions.getValue(e, "name"));
        map.put("Score", "Score: " + XMLfunctions.getValue(e, "score"));
        mylist.add(map);            
    }       

    ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main, 
                    new String[] { "name", "Score", "name" }, 
                    new int[] { R.id.item_title, R.id.item_subtitle, R.id.item_subtitle2 });

    setListAdapter(adapter);

    final ListView lv = getListView();
    lv.setTextFilterEnabled(true);  
    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
            @SuppressWarnings("unchecked")
            HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);                   
            Toast.makeText(Main.this, "ID '" + o.get("name") + "' was clicked.", Toast.LENGTH_LONG).show(); 

        }
    });

    return true;
}
}

I use the following code, to open a XML file and view its info in a ListView.
I have included a ProgressDialog in the first part of my code, in order to show it until files loading and once they are loaded the ProgressDialog disappears.
The problem is that ProgressDialog is being shown when files are already loaded. The screen is black till files are being loaded and when they are completely loaded, ListView is being displayed and at the same time ProgressDialog displays.
Please help me.
Thanks

public class Main extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    final ProgressDialog _progressDialog = new ProgressDialog(this);
    _progressDialog.setIcon(R.drawable.icon);
    _progressDialog.setTitle("Loading ...");
    _progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);

    runOnUiThread(new Runnable() {

        @Override
        public void run() {
            _progressDialog.show();
             Thread t = new Thread() {
                  public void run(){
                      GetData();
                      _progressDialog.dismiss();
                    }
                  };
                  t.run();
        }
    });

}

public boolean GetData() {

    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

    setContentView(R.layout.listplaceholder);

    String xml = XMLfunctions.getXML();
    Document doc = XMLfunctions.XMLfromString(xml);

    int numResults = XMLfunctions.numResults(doc);

    if((numResults <= 0)){
        Toast.makeText(Main.this, "Geen resultaten gevonden", Toast.LENGTH_LONG).show();  
        finish();
    }

    NodeList nodes = doc.getElementsByTagName("result");

    for (int i = 0; i < nodes.getLength(); i++) {                           
        HashMap<String, String> map = new HashMap<String, String>();    

        Element e = (Element)nodes.item(i);
        map.put("id", XMLfunctions.getValue(e, "id"));
        map.put("name", "Naam:" + XMLfunctions.getValue(e, "name"));
        map.put("Score", "Score: " + XMLfunctions.getValue(e, "score"));
        mylist.add(map);            
    }       

    ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main, 
                    new String[] { "name", "Score", "name" }, 
                    new int[] { R.id.item_title, R.id.item_subtitle, R.id.item_subtitle2 });

    setListAdapter(adapter);

    final ListView lv = getListView();
    lv.setTextFilterEnabled(true);  
    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
            @SuppressWarnings("unchecked")
            HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);                   
            Toast.makeText(Main.this, "ID '" + o.get("name") + "' was clicked.", Toast.LENGTH_LONG).show(); 

        }
    });

    return true;
}
}

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

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

发布评论

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

评论(2

故事灯 2024-12-18 16:08:11

使用 AsyncTask 并在 onPreExecute()

public class Main extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    final ProgressDialog _progressDialog = new ProgressDialog(this);
    _progressDialog.setIcon(R.drawable.icon);
    _progressDialog.setTitle("Loading ...");
    _progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);

    new AsyncTask<Void, Void, Void>() {
     protected Long doInBackground(Void ... urls) {
         GetData();
     }

     protected void onPostExecute(Void result) {
         _progressDialog.dismiss();
     }

     protected void onPreExecute(Void no) {
         _progressDialog.show();
     }

    }.execute();

}

Use AsyncTask and show the progress dialog in onPreExecute():

public class Main extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    final ProgressDialog _progressDialog = new ProgressDialog(this);
    _progressDialog.setIcon(R.drawable.icon);
    _progressDialog.setTitle("Loading ...");
    _progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);

    new AsyncTask<Void, Void, Void>() {
     protected Long doInBackground(Void ... urls) {
         GetData();
     }

     protected void onPostExecute(Void result) {
         _progressDialog.dismiss();
     }

     protected void onPreExecute(Void no) {
         _progressDialog.show();
     }

    }.execute();

}
萌辣 2024-12-18 16:08:11

根据您是否准备好使用新的 Fragments API,您还可以扩展 ListFragment(使用 android 兼容包) 并使用 setListShown(布尔值)。我经常使用 LoaderManager.LoaderCallbacks; (与您的上下文不完全相同,也许您可​​以使用 AsyncTaskLoader )。

无论如何:AsyncTask 响应+1。

Depending on the fact that you are ready or not to use the new Fragments API you could also extends a ListFragment (using the android compatibility package) and use setListShown(boolean). I use that a lot with LoaderManager.LoaderCallbacks<Cursor> (not the very same context as yours, maybe you could use an AsyncTaskLoader).

Anyway: +1 for the AsyncTask response.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文