如何在列表视图对话框弹出窗口上显示加载进度条?

时间:2016-03-15 02:57:52

标签: android listview dialog progressdialog

我有一个列表视图,类型对话框弹出窗口。当我点击列表视图的项目时,我想要显示加载进度条。下面是listview代码。

public AdapterView.OnItemClickListener getOnItemClickListener() {
        return new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                ProgressDialog dialog;
//                dialog = ProgressDialog.show(view.getContext(), "Please wait..", "Loading data", true);
//                dialog.setCancelable(false);
//                dialog.show();
                HashSet sometable= new HashSet();
                sometable.add(i);
                if (getContext() == null) return;
                Intent output = new Intent();
                final IData data = (IData)  adapterView.getItemAtPosition(i);
                //check if its myself
                if((data.get("FIRST_NAME")==null)&&(data.get("LAST_NAME")==null))
                output.putExtra("User","");
                else{
                    output.putExtra("User", (String) data.get("ASSIGNEE_GUID"));
                    output.putExtra("userName", (String) data.get("FIRST_NAME") + " " + (String) data.get("LAST_NAME"));
                    //new cancelPending((Activity) getContext(),data).execute();
                    AssigneABO.SyncSubordinateCalendar((String) data.get("ASSIGNEE_GUID"), view.getContext());
                    //new MyTask(MainActivity.this).execute((Void) null);

                }
                Activity activity = (Activity) getContext() ;
                activity.setResult(Activity.RESULT_OK,output);
                activity.finish();
            }
        };
    }

当我点击列表视图的项目时,启动异步任务以获取数据。在该异步任务的onprexcute方法中,我正在调用进度对话框,如下所示。

public static class SyncApplicationTask extends AsyncTask<String, Context, Boolean> {
        Context context;
        ProgressDialog progress;// = new ProgressDialog(context);
        public SyncApplicationTask(Context mcontext) {
            super();
            context = mcontext;
            progress = new ProgressDialog(context);
        }

        @Override
        protected void onPreExecute() {

            progress.setMessage(context.getString(R.string.Sync));
            progress.setTitle(R.string.sync_msg);
            progress.setCanceledOnTouchOutside(false);
            progress.setCancelable(false);
            progress.setIndeterminate(true);
            progress.show();

            if (!Util.hasActiveNetworkConnection(context)) {
                Util.alertMessageDialog(context, R.string.network_connection_unavailable_message);
             return;
            }
            if (SynchronizationManager.getInstance().isSyncAllCategoryGroupActive()) {
                Util.alertMessageDialog(context, R.string.synchronization_still_active);
                return;
            }
            if (!ConnectionManager.getInstance().isServerConnected()) {
                ConnectionManager.getInstance().stopServerConnection(60);
                boolean isStarted = ConnectionManager.getInstance()
                        .startServerConnection(60); // 2 mins time out
                Log.v("Connection started : ", Boolean.valueOf(isStarted)
                        .toString());
                if (!isStarted) {
                    Util.alertMessageDialog(context, R.string.connection_server_lost_message);
                    return;
                }
            }
        }

        @Override
        protected Boolean doInBackground(String... syncGroups) {
            try {
                SAPReXDB.synchronize(syncGroups[0]);

                return true;
            }catch (Exception Ex)
            {  Log.v("Erron on sync: ", Ex.toString());
                return false;
            }
        }

        @Override
        protected void onPostExecute(final Boolean c) {
            progress.dismiss();
            if(!c)
                Util.alertMessageDialog(context, R.string.sync_progress_failed);

        }
    }

但它显示在对话框列表视图后面。我希望它显示在对话框列表视图上方。我究竟做错了什么。你能告诉我这个吗?谢谢。

1 个答案:

答案 0 :(得分:1)

您使用哪个Context创建ProgressDialog?
GUI位置&amp;如果您不使用正常的活动上下文,但使用特定的视图上下文或应用程序上下文,则可能会发生订单问题。