【问题标题】:keep a ProgessDialog until the callback is executed保持 ProgessDialog 直到回调被执行
【发布时间】:2014-05-14 20:31:55
【问题描述】:

我正在使用 azure 移动服务。我在数据库中有一些用户要进行身份验证,为了做到这一点,我在输入用户名和密码并按 OK 后执行查询以获取用户。当按下 OK 时,如果一切顺利,应该开始一个意图。如何在执行查询的回调方法完成之前显示 ProgressDialog?

编辑:问题是我有一个按钮(登录按钮),当您单击它时,它将构建一个查询并在异步任务中执行它,因此是我的问题。如果我只是添加一个进度对话框,调用流程将继续进行,因为从 onClickListener 的角度来看,操作已经完成。

【问题讨论】:

  • 发布一些代码,如果他们使用异步任务,您在哪里获得数据准备就绪的回调,因为回调所在的位置是您应该关闭 pialog 的位置
  • 它不是一个 android AsyncTask 那样工作。我可以在回调中关闭它,它会起作用,但其余代码仍将被执行,所以基本上这对我没有帮助。
  • 你需要在出现问题的地方显示一些代码

标签: android android-progressbar azure-mobile-services


【解决方案1】:

在调用查询之前显示()它并在回调方法中关闭()它。

【讨论】:

    【解决方案2】:

    当您使用AsyncTask 查询数据时,请使用onPreExecuteonPostExecute 方法来显示/关闭ProgressDialog

    创建一个扩展 AsyncTask 的类,像这样。在onPreExecute 中显示ProgressDialog,当您完成在doInBackground 中获取数据时,在onPostExecute 中关闭对话框

       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();
         } 
      }
    

    最后,当按钮onClick执行任务时

      new QueryTask(YourActivity.this).execute();
    

    【讨论】:

    • 不,我没有使用 AsyncTask 来查询数据。我正在使用他们的 api(azure 移动服务 api),它是围绕异步调用实现的
    【解决方案3】:

    我使用此示例代码从 SQL 数据库加载所有事件。在应用从服务器获取数据之前,会向用户显示一个进度对话框。

    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);
                        }
                    });
    
                }
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-19
      • 1970-01-01
      • 2014-02-06
      • 2013-11-17
      • 2018-07-24
      • 1970-01-01
      • 1970-01-01
      • 2021-02-10
      相关资源
      最近更新 更多