2

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.

nurealam11
  • 537
  • 4
  • 16
diazazar
  • 522
  • 1
  • 5
  • 24
  • 2
    post some code where do you get the callback that the data is ready if they use an asynctask because where that callback is is where you should be dismissing the pialog – tyczj May 29 '14 at 20:50
  • its not an android AsyncTask to work like that. i can dismiss it in the callback and it will work, but the rest of the code will still get executed so basically that doesn't help me at all. – diazazar May 30 '14 at 11:42
  • You need to show some code where the problem occurs – tyczj May 30 '14 at 12:00

3 Answers3

4

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

nasch
  • 5,330
  • 6
  • 31
  • 52
2

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();
Libin
  • 16,967
  • 7
  • 61
  • 83
  • no, I am not using the AsyncTask to query the data. I am using their api(azure mobile service api) which is implemented around an async call – diazazar May 29 '14 at 20:26
1

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.

Michele La Ferla
  • 6,775
  • 11
  • 53
  • 79