简体   繁体   中英

Asynctask progress dialog

I have a problem moving some of my initial code into an Asynctask.

I've been reading through several Asynctask examples as well as the Android developer article on Asynctask but I'm finding it hard to relate it to my code in a away I can understand.

The code below gives me an error and force closes when executing doInBackground(), and is Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

public class LoadingDialog extends AsyncTask<ArrayList<HashMap<String, String>>, Void, ArrayList<HashMap<String, String>>>
{

    ProgressDialog pDialog = new ProgressDialog(Home.this);


    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        pDialog = ProgressDialog.show(Home.this, "", "Loading...");

    }

    protected ArrayList<HashMap<String, String>> doInBackground(ArrayList<HashMap<String, String>>... params) {
        // TODO Auto-generated method stub
        finalList.clear();
        eventList = database.getEvents();
        venueList = database.getVenues();
        finalList = createFinalList(eventList, venueList);

        return finalList;
    }

    @Override
    protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
        // TODO Auto-generated method stub
        pDialog.dismiss();

        ListAdapter adapter = new SimpleAdapter(Home.this, finalList , R.layout.home,
                  new String[] { "eventName", "venueName", "startTime"},
                  new int[] { R.id.item_title, R.id.item_subtitle, R.id.item_datetime });
                  setListAdapter(adapter);

                  lv = getListView();
                  lv.setTextFilterEnabled(true);

                  lv.setOnItemClickListener(new OnItemClickListener()
                  {
                      public void onItemClick(AdapterView<?> parent, View view, int position, long id)
                      {
                          Intent i = new Intent(Home.this, EventInfo.class);

                          HashMap<String, String> map = finalList.get(position);
                          String eID = map.get("eventId");
                          String name = map.get("eventName");
                          String venue = map.get("venueName");
                          String lati = map.get("venueLatitude");
                          String longi = map.get("venueLongitude");
                          String opening = map.get("openingTimes");
                          String descrip = map.get("venueDescription");
                          String vID = map.get("venueId");
                          String email = map.get("venueEmail");
                          String phone = map.get("venuePhone");
                          String facebook = map.get("venueFacebook");
                          String twitter = map.get("venueTwitter");

                          i.putExtra("itemId", eID);
                          i.putExtra("itemName", name);
                          i.putExtra("venueName", venue);
                          i.putExtra("venueLati", lati);
                          i.putExtra("venueLong", longi);
                          i.putExtra("opening", opening);
                          i.putExtra("venueDescription", descrip);
                          i.putExtra("venueID", vID);
                          i.putExtra("venueEmail", email);
                          i.putExtra("venuePhone", phone);
                          i.putExtra("venueFacebook", facebook);
                          i.putExtra("venueTwitter", twitter);
                          startActivityForResult(i, 0);               

                      }
                  });

    }


}

This is part of the home class:

public class Home extends ListActivity {
int eventid;
String[] venue = null;
String[] name = null;
private SqlParser database = new SqlParser();
private ArrayList<HashMap<String, String>> eventList = new ArrayList<HashMap<String, String>>();
private ArrayList<HashMap<String, String>> venueList = new ArrayList<HashMap<String, String>>();
private ArrayList<HashMap<String, String>> finalList = new ArrayList<HashMap<String, String>>();
ListView lv = null;

I would really appreciate any help.

create this line ProgressDialog pDialog = new ProgressDialog(Home.this); in out side the AsynTask class

Just check your code:

 @Override
    protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
        // TODO Auto-generated method stub
        pDialog.dismiss();

        ListAdapter adapter = new SimpleAdapter(Home.this, finalList , R.layout.home,
                  new String[] { "eventName", "venueName", "startTime"},
                  new int[] { R.id.item_title, R.id.item_subtitle, R.id.item_datetime });
                  setListAdapter(adapter);

                  lv = getListView();

Here you are trying to set adapter to listview before getting ListView actually.

It should be like:

 lv = getListView();
 setListAdapter(adapter);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM