简体   繁体   English

在Android Studio中显示和隐藏对话框

[英]show and hide dialog in android studio

I am having a situation that my dialog doesn't show up in my asyncTask. 我的对话框没有显示在asyncTask中。 The codes below are my asyncTask 下面的代码是我的asyncTask

private class AsyncCallListWS extends AsyncTask<Void, Void, Void> {
@Override
    protected void onPreExecute() {
        Log.i(TAG, "--------------------------------------------------");
        Log.i(TAG, "pending ws: onPreExecute");
        showLoadingDialog();
    }

    @Override
    protected Void doInBackground(Void... params) {
        Log.i(TAG, "pending ws: doInBackground");
        //listDataParent = new ArrayList<Tn_Parent>();
        listPending();
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        Log.i(TAG, "Call pending ws: onPostExecute");
        dismissLoadingDialog();

        //Log.i(TAG, "I am not up there "+status.toString());
        if(getContext()!=null) {
            //adapter = new Tn_ListViewAdapter(getActivity(), newList, selectAll);
            //listView.setAdapter(adapter);

            lvAdapter = new Tn_ListViewAdapter(getActivity(), lvList, selectAll);
            listView.setAdapter(lvAdapter);
            // .............. below is not needed .....................
            //listAdapter = new Tn_ExpandableAdapter(listDataParent,getContext(), selectAll);
            //expListView.setAdapter(listAdapter);
        }
    }
}

And below is my dialogbox codes. 下面是我的对话框代码。 The dialog work well in other class. 该对话框在其他课程中效果很好。

public void showLoadingDialog() {

    if (bar == null) {
        bar = new ProgressDialog(getActivity());
        bar.setMessage(getString(R.string.loading_message));
        //bar.setCanceledOnTouchOutside(getRetainInstance());
        bar.setCanceledOnTouchOutside(false);
    }
    bar.show();
}

public void dismissLoadingDialog() {

    if (bar != null && bar.isShowing()) {
        bar.dismiss();
    }
}

I really wish to know what are the problems. 我真的很想知道有什么问题。 The dialog show up when I put showLoadingDialog() in the onCreateView(), but the problem is that the dialog will not dismiss if i put it inside the onCreateView(). 当我将showLoadingDialog()放在onCreateView()中时,将显示该对话框,但是问题是,如果将对话框放在onCreateView()中,该对话框将不会消失。 Please help. 请帮忙。

For your dismiss() problem inside onCreate , try to change 对于onCreate中的 dismiss()问题,请尝试更改

public void dismissLoadingDialog() {
  if (bar != null && bar.isShowing()) {
      bar.dismiss();
  }
}

to

public void dismissLoadingDialog() {
  if (bar != null) {
      bar.dismiss();
      bar = null;
  }
}

The problem to not showing up your dialog inside AsynTask might be your if (bar == null) { condition, because at that time your bar object will not be null. 无法在AsynTask中显示对话框的问题可能是您的if(bar == null){条件,因为那时您的bar对象不会为null。 So that the time when you are dismissing the dialog you have to initialize it to null . 因此,在关闭对话框时,必须将其初始化为null And please write bar.show(); 并且请编写bar.show(); this line of code inside if(...) condition. if(...)条件中的这一行代码。

As Preetika Kaur suggested you should pass a Context object to you showLoadingDialog() and call bar = new ProgressDialog(yourContextObject); 正如Preetika Kaur建议的那样,您应该将Context对象传递给showLoadingDialog()并调用bar = new ProgressDialog(yourContextObject); cause otherwise the bar would always be null . 否则,bar将始终为null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM