Skip to content Skip to sidebar Skip to footer

Android Asynctask, Prevent UI From Freezing

I have this peace of code, that fetches some xml data from an url. It does it's job properly but the problem is that while it downloads and parses xml the UI freezes, and that mig

Solution 1:

try following code:

private class LoadMoreListView extends AsyncTask<Void, Void, ArrayList<HashMap<String, String>>> {

@Override
protected void onPreExecute() {
    // Showing progress dialog before sending http request
    pDialog = new ProgressDialog(
            AndroidListViewWithLoadMoreButtonActivity.this);
    pDialog.setMessage("Please wait..");
    pDialog.setIndeterminate(true);
    pDialog.setCancelable(false);
    pDialog.show();
}

protected ArrayList<HashMap<String, String>> doInBackground(Void... unused) {

      ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
//MOTHERLAJME MULTIDIMENSIONAl
final List<HashMap<String, String>> MotherContainer= new ArrayList<HashMap<String, String>>();


try{ 

    XMLParser parser = new XMLParser();
    String xml   = parser.getXmlFromUrl(catUrl); // getting XML
    Document doc = parser.getDomElement(xml); // getting DOM element


    //ALL
    NodeList forecastW = doc.getElementsByTagName("newsitem");

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

        Node nodeday = forecastW.item(j);
        Element dayElmnt = (Element) nodeday;

        map.put("title", (parser.getValue(dayElmnt, "title")) );
        map.put("intro", (parser.getValue(dayElmnt, "intro")).toString());

        map.put("story_id", ""+j ); 

        // adding HashList to ArrayList
        menuItems.add(map);


        //MULTI DIMENSIONAL ARRAY
        HashMap<String, String> TheStory= new HashMap<String, String>();
        TheStory .put("title", (parser.getValue(dayElmnt, "title")));
        TheStory .put("story_date", parser.getValue(dayElmnt, "datetime"));
        MotherContainer.add(j, TheStory);
        /////////////////////////

    }


} catch (Exception e) {
    System.out.println("XML Pasing Excpetion = " + e);
}


    return menuItems;
}


protected void onPostExecute(ArrayList<HashMap<String, String>> unused) {
    // closing progress dialog
    pDialog.dismiss();


     // Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(YourActivity.this, unused,
        R.layout.week_day_item,
        new String[] { "title", "intro", "story_id"}, new int[] {
                R.id.title_list,
                R.id.intro_list,
                R.id.story_id_list});

setListAdapter(adapter);


}

In AsyncTask

1st param means the type you can pass to execute. Void means you can pass nothing

The class names in Java should start with upper-calse letter. Please rename it for better readability by others.

So proper call would be

new LoadMoreListView().execute();

2nd param is a type of data you can publish calling publishProgress() from doInBackground(). You don't use publishProgress, so nothing to mention in this case.

3rd param mean s type that will be passed to onPostExecute(). To pass the menuItems to onPostExecute you must return it from doInBackground. so you need declare your class with

AsyncTask<Void, Void, ArrayList<HashMap<String, String>>>

doInBackground runs on new thread so you don't need following code:

new Runnable() {
            public void run()

if you want work with UI thread you can use following method:

  1. onPreExecute

  2. onPostExecute

onPreExecute usually used for showing please wait dialog or something like that and onPostExecute used for showing data after downloading and other thing


Post a Comment for "Android Asynctask, Prevent UI From Freezing"