Question

im using the azure mobile service. I have some users in the db i want to authenticate, and in order to do that, I execute a query to get a User after you enter a username and a password and press OK. When OK is pressed, if all it's well an intent should be started. How can I display a ProgressDialog until the callback method of the executed query is completed?

EDIT: the problem is that i have a button(logIn button) and when you click it, it will build a query and execute it in an async task, hence my problem. If i just add a progress dialog the call flow will move on since from the onClickListener point of view, the action has finished.

Was it helpful?

Solution 3

This example code was used by me to load all the events from an SQL database. Until the app gets the data from the server, a progress dialog is displayed to the user.

class LoadAllEvents extends AsyncTask<String, String, String> {

            /**
             * Before starting background thread Show Progress Dialog
             * */
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(getActivity());
                pDialog.setMessage("Just a moment...");
                pDialog.setIndeterminate(true);
                pDialog.setCancelable(true);
                pDialog.show();
            }

            protected String doInBackground(String... args) {
                // Building Parameters
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                // getting JSON string from URL
                JSONObject json = jParser.makeHttpRequest(url_all_events,
                        "GET", params);

                try {
                    // Checking for SUCCESS TAG
                    int success = json.getInt(CONNECTION_STATUS);

                    if (success == 1) {
                        // products found
                        // Getting Array of Products
                        Events = json.getJSONArray(TABLE_EVENT);
                        // looping through All Contacts
                        for (int i = 0; i < Events.length(); i++) {
                            JSONObject evt = Events.getJSONObject(i);

                            // Storing each json item in variable
                            id = evt.getString(pid);
                            group = evt.getString(COL_GROUP);
                            name = evt.getString(COL_NAME);
                            desc = evt.getString(COL_DESC);
                            date = evt.getString(COL_DATE);
                            time = evt.getString(COL_TIME);

                            // creating new HashMap
                            HashMap<String, String> map = new HashMap<String, String>();

                            // adding each child node to HashMap key => value
                            map.put(pid, id);
                            map.put(COL_GROUP, group);
                            map.put(COL_NAME, name);
                            map.put(COL_DESC, desc);
                            map.put(COL_DATE, date);
                            map.put(COL_TIME, time);

                            // adding HashList to ArrayList
                            eventsList.add(map);
                        }
                    } else {
                        // Options are not available or server is down.
                        // Dismiss the loading dialog and display an alert
                        // onPostExecute
                        pDialog.dismiss();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                return null;
            }

            protected void onPostExecute(String file_url) {
                // dismiss the dialog after getting all products
                pDialog.dismiss();
                // updating UI from Background Thread
                getActivity().runOnUiThread(new Runnable() {
                    public void run() {
                        ListAdapter adapter = new SimpleAdapter(getActivity(),
                                eventsList, R.layout.list_item, new String[] {
                                        pid, COL_GROUP, COL_NAME, COL_DATE, COL_TIME },
                                new int[] { R.id.pid, R.id.group, R.id.name, R.id.header,
                                        R.id.title2 });

                        setListAdapter(adapter);
                    }
                });

            }

hope this helps.

OTHER TIPS

Just show() it before you call the query and dismiss() it in the callback method.

As your using the AsyncTask to query the data , use the onPreExecute and onPostExecute methods to show/dismiss the ProgressDialog.

Create a class which extends the AsyncTask , like this . In the onPreExecute show the ProgressDialog and when your done with fetching the data in doInBackground , in onPostExecute dismiss the dialog

   public class QueryTask extends AsyncTask<Void,Void,Object> {

        private ProgressDialog progressDialog = null;
        private final Context mContext;

        public QueryTask(Context context) {
            mContext = context;
        }

       @Override
        protected void onPreExecute() {
           progressDialog = new ProgressDialog(mContext);
           progressDialog.show();
       }

      @Override
      protected Void doInBackground(Void... params) {
         // do your stuff to query the data
         return null;
      }
      @Override
      protected void onPostExecute(Object result) {
         progressDialog.dismiss();
        // do your other stuff with the queried result

      }
      @Override
      protected void onCancelled(Object result) {
         progressDialog.dismiss();
     } 
  }

Finally, when button onClick execute the task

  new QueryTask(YourActivity.this).execute();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top