0

I have a custom List which I want to refresh when a action bar button is clicked. This List is within a Fragment.

I want to show a ProgressBar until the List is refreshed.

Currently Iam doing this:-

private class RefreshList extends AsyncTask<Void, Void, Void>
{

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);

        if(refDialog!=null)
        {
            refDialog.dismiss();
        }
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();

        if(refDialog!=null)
        {
            refDialog =null;
        }
        refDialog =  WaitProgressFragment.newInstance();

        refDialog.show(getFragmentManager(), "Wait");


    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        //stagAdaper.notifyDataSetChanged();

        itemsAdapter.notifyDataSetChanged();



        return null;
    }

}

But I get this error

Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

How can I refresh the List while showing the progress dialog?

user3146095
  • 419
  • 2
  • 7
  • 25
  • Check: http://stackoverflow.com/questions/5161951/android-only-the-original-thread-that-created-a-view-hierarchy-can-touch-its-vi – Paresh Mayani Jul 15 '14 at 11:20

2 Answers2

0

Put

itemsAdapter.notifyDataSetChanged();

Inside onPostExecute.

notifyDataSetChanged is an UI operation and doInBackground does not work on UI thread, whereas onPostExecute does.

vipul mittal
  • 17,343
  • 3
  • 41
  • 44
0

You have to move the portion of the background task to the UI thread,so notify itemsAdapter in onPostExecute method as

  @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        itemsAdapter.notifyDataSetChanged()

        if(refDialog!=null)
        {
            refDialog.dismiss();
        }
    }
Giru Bhai
  • 14,370
  • 5
  • 46
  • 74