I love coding
I love coding

Reputation: 1191

How to display ProgressDialog bar within a fragment?

I want to display a ProgressDialog during an AsyncTask, executed from a Fragment. The problem I'm facing, it is that the progress dialog isn't displayed during the AsyncTask and I don't know why.

Here is the AsyncTask:

public class RankTask extends AsyncTask<Void, Void, ArrayList<RowRank>> {

    private ProgressDialog pDialog;
    private Context context;

    public RankTask(Context context) {
        super();
        this.context = context;

    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(context);
        pDialog.setMessage("Loading...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();

    }

    @Override
    protected ArrayList<RowRank> doInBackground(Void... params) {

        IDBInteraction interaction = new DBInteraction(
                Connection.getConnection());

        return interaction.getRank();
    }

    @Override
    protected void onPostExecute(ArrayList<RowRank> result) {

        pDialog.dismiss();
        super.onPostExecute(result);
    }

}

Here is the Fragment, which executes the AsyncTask:

public class RankFragment extends Fragment {

private TableLayout tableLayout;
private static float SIZE_TEXT = 20;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(
            com.example.testintro.R.layout.fragment_rank, container, false);
    tableLayout = (TableLayout) rootView
            .findViewById(R.id.table_rank_layout);
    tableLayout.setStretchAllColumns(true);
    tableLayout.bringToFront();

    RankTask task = new RankTask(getActivity());
    try {

        createTable(task.execute().get());

    } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }
    return rootView;
}

Here is the line of code of Activity, where the Fragment is commited:

private void changeActivity(Button button, final Fragment fragment) {
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager
                    .beginTransaction();
            fragmentTransaction.replace(R.id.container, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();

        }
    });

Upvotes: 1

Views: 523

Answers (2)

Amitabha Biswas
Amitabha Biswas

Reputation: 3291

I guess.. Its due to 'context' problem.. do passing context properly..

Added this code at your Fragment..

   public Context context;
    @Override
    public void onAttach(Activity activity)
    {
        super.onAttach(activity);
        context = activity;
    }

Upvotes: 1

Ahmed Basyouny
Ahmed Basyouny

Reputation: 343

It is nothing to do with progress dialog or the fragment the only thing that is wrong in your could is that you are using

task.execute().get()

which will Waits if necessary for the computation to complete, and then retrieves its result.

and as it run on main thread you can't use any views remove the get() and it should works fine

Upvotes: 1

Related Questions