在加载XML文件之前显示progressDialog

时间:2011-10-21 18:46:25

标签: android listview progressdialog

我使用以下代码打开XML文件并在ListView中查看其信息 我在代码的第一部分中包含了一个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;
}
}

2 个答案:

答案 0 :(得分:1)

使用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();

}

答案 1 :(得分:0)

根据您准备好或不使用新Fragments API的事实,您还可以扩展ListFragment(使用android compatibility package)并使用setListShown(boolean)。我在LoaderManager.LoaderCallbacks&lt; Cursor&gt;中使用了很多东西。 (与你的背景不同,也许你可以使用AsyncTaskLoader)。

无论如何:AsyncTask响应为+1。