0

I have a problem with AsyncTask.

I update a list of object by calling a webService. During this time, i want to display a ProgressDialog. Then when the list is fully updated, i want the dialog the close and the AsyncTask too.

The problem is that the UIThread doesn't wait the AsyncTask to finish. I see that the get() method can work for me, but with it, the UIThread is just blocked, and the dialog just doesn't show.

I would like to know how i can manage to wait the task to finish while displaying a ProgressDialog.

Here is my AsyncTask Class :

public class MyAsyncTask extends AsyncTask<Integer, Void, Boolean>{

private ProgressDialog  pd;

private String          m_sShelfCode;
private String          m_sFamiliyCode;

public MyAsyncTask(String shelfCode, String familyCode) {
    this.m_sShelfCode = shelfCode;
    this.m_sFamiliyCode = familyCode;
}

@Override
protected void onPreExecute() {
    AppManager app = AppManager.getInstance();
    LayoutInflater inflater = (LayoutInflater)app.m_AppContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    RelativeLayout rl = (RelativeLayout)inflater.inflate(R.layout.progress_dialog, null,  false);
    pd = new ProgressDialog(app.m_AppContext);
    pd.requestWindowFeature(Window.FEATURE_NO_TITLE);
    pd.setCancelable(false);
    pd.show();
    pd.setContentView(rl);
}

@Override
protected Boolean doInBackground(Integer... params) {
    Log.d("DO IN BACKGROUND","param[0]="+params[0]);
    switch (params[0]) {
    case AppManager.SHELF_LIST:
        //Calling WebService
        SoapShelf s = new SoapShelf();
        try {
            ArticleListFragment.m_ShelfList = s.getShelfList();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } catch (XmlPullParserException e) {
            e.printStackTrace();
            return false;
        }
        break;
    case AppManager.FAMILY_LIST:
        // set family list
        break;
    case AppManager.ARTICLE_LIST:
        // set article list
        break;
    }
    return true;
}

@Override
protected void onPostExecute(Boolean result) {
    pd.dismiss();
    if (result == false)
        ;// error dialog
}
}

I call this in my ArticleListFragment here

public void getItemList(int listType) {
    switch (listType) {
    case AppManager.SHELF_LIST:
            new MyAsyncTask(null, null).execute(new Integer[]{AppManager.SHELF_LIST});

Thanks in advance

Bernie
  • 173
  • 3
  • 13

1 Answers1

1

try with:

@Override
protected void onPreExecute() {
    pd = ProgressDialog.show(context, "title", "message");
}

@Override
protected void onPostExecute(Boolean result) {
  if (pd != null && pd.isShowing())
    pd.dismiss();
}
kjurkovic
  • 4,044
  • 2
  • 23
  • 28
  • Same as before :s. When I set the adapter to this list after calling AsyncTask, list is empty. When i refresh my list, list is filled. You think the problem may come from the dialog? – Bernie Sep 03 '13 at 13:54
  • you should call adapter.notifyDataSetChanged() in onPostExecute if data of the adapter has changed. or set list adapter onPostExecute – kjurkovic Sep 03 '13 at 13:57
  • Wow, it was it. I was so dumb. Anyways, thanks dude. I'm out off my own mess. – Bernie Sep 03 '13 at 14:16