user2351519
user2351519

Reputation:

Display ProgressDialog in ListFragment

I have a ListFragment with a custom ListView. In "onCreateView" I'm inflating the view: View rootView = inflater.inflate(R.layout.fragment_subst, container, false);. Now in "onActivityCreated" I'm setting the adapter and downloading some data like this:

MyAdapter adapter = new MyAdapter(getActivity(), new GetData().execute().get());

setListAdapter(adapter);

Downloadiing takes its time and the user only see a white page with an ActionBar. This is very bad, so I added a ProgressDialog:

ProgressDialog dialog = ProgressDialog.show(getActivity(), "Loading...", "Please wait...", true);

MyAdapter adapter = new MyAdapter(getActivity(), new GetData().execute().get());

setListAdapter(adapter);

dialog.dismiss();

But the ProgressDialog isn't shown. What exactly is the problem? I've inflated the view before showing the dialog...

Thanks!

Upvotes: 3

Views: 5016

Answers (2)

Hemanth S
Hemanth S

Reputation: 688

I just modified the code before to use create global variable

before onPreExcute()

ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = ProgressDialog.show(getActivity(), "Loading...", "Please wait...", true);
    }

onPostExcute hide the dialog

 @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        dialog.hide();
    }

Upvotes: 0

Jitender Dev
Jitender Dev

Reputation: 6925

In your AsyncTask GetData

Show progress dialog in your onPreExecute method

ProgressDialog dialog = ProgressDialog.show(getActivity(), "Loading...", "Please wait...", true);

and dismiss the dialog in onPostExecute.

Upvotes: 3

Related Questions