maxoya
maxoya

Reputation: 53

Show progressDialog before load XML file

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;
}
}

Upvotes: 1

Views: 1305

Answers (2)

Renaud
Renaud

Reputation: 8873

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.

Upvotes: 0

MByD
MByD

Reputation: 137382

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

}

Upvotes: 1

Related Questions