3

I'm developing my first Android application and i want to show a progress dialog until a xml file is being processed in doInBackground method. This activity is loaded in response to an onclick event. Unexpectedly, activity takes several seconds to show up and the progress dialog is shown for a very little time (several milliseconds).

This is my code. I have used AsyncTask as an inner class. I just can't find where i have gone wrong.

public class WhatToSee extends ListActivity {
ListView whatToSee;
ArrayList<HashMap<String, String>> whatToSeeInfo = new ArrayList<HashMap<String, String>>();
WhatToSeeAdapter adapter;
String cityName;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.whattosee);
    cityName = getIntent().getStringExtra("name");
    whatToSee = (ListView) findViewById(android.R.id.list);
    adapter= new WhatToSeeAdapter(this, whatToSeeInfo);
    whatToSee.setAdapter(adapter);
    new WhatToSeeLoader().execute();
}




public class WhatToSeeLoader extends AsyncTask<Void, String, String> {
    ProgressDialog progress = new ProgressDialog(WhatToSee.this);
    String url = "http://wearedesigners.net/clients/clients12/tourism/fetchWhatToSeeList.php";
    final String TAG_MAIN = "item";
    final String TAG_ID = "itemId";
    final String TAG_NAME = "itemName";
    final String TAG_DETAIL = "itemDetailText";
    final String TAG_MAP = "itemMapData";
    final String TAG_ITEM_IMAGE = "itemImages";
    final String TAG_MAP_IMAGE = "mapImage";
    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(url); // getting XML
    Document doc = parser.getDomElement(xml); // getting DOM element
    NodeList nl = doc.getElementsByTagName(TAG_MAIN);

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        progress.setMessage("Loading What To See List");
        progress.setIndeterminate(true);
        progress.show();

    }

    @Override
    protected String doInBackground(Void... params) {
        // TODO Auto-generated method stub
        for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);
            // adding each child node to HashMap key => value
            map.put(TAG_ID, parser.getValue(e, TAG_ID));
            map.put(TAG_NAME, parser.getValue(e, TAG_NAME));
            map.put(TAG_DETAIL, parser.getValue(e, TAG_DETAIL));
            map.put(TAG_MAP, parser.getValue(e, TAG_MAP));
            map.put(TAG_MAP_IMAGE, parser.getValue(e, TAG_MAP_IMAGE));
            map.put(TAG_ITEM_IMAGE, parser.getValue(e, TAG_ITEM_IMAGE));

            // adding HashList to ArrayList
            whatToSeeInfo.add(map);
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        adapter.notifyDataSetChanged();
        progress.dismiss();

    }

}

}

Can someone please help me with this? Thank you in advance.

pathfinder
  • 39
  • 3
  • A similar questions has been asked in this thread. Still i can't find a proper answer there. http://stackoverflow.com/questions/2702695/android-async-task-progressdialog-isnt-showing-until-background-thread-finishes – pathfinder Nov 01 '12 at 15:03

1 Answers1

2

Most probably call String xml = parser.getXmlFromUrl(url); // getting XML slows down Your code. It get executed in main thread and I expect it (from its name) to do some network related staff which basically should be done not in UI thread (e.g. in Yours doInBackground method).
So, to fix try to move that initialization staff related to xml inside doInBackground().

sandrstar
  • 12,503
  • 8
  • 58
  • 65