![](/img/trans.png)
[英]How to stop the “Countdown timer ” and show the result in dialog in Android studio?
[英]show and hide dialog in android studio
我的對話框沒有顯示在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);
}
}
}
下面是我的對話框代碼。 該對話框在其他課程中效果很好。
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();
}
}
我真的很想知道有什么問題。 當我將showLoadingDialog()放在onCreateView()中時,將顯示該對話框,但是問題是,如果將對話框放在onCreateView()中,該對話框將不會消失。 請幫忙。
對於onCreate中的 dismiss()問題,請嘗試更改
public void dismissLoadingDialog() {
if (bar != null && bar.isShowing()) {
bar.dismiss();
}
}
至
public void dismissLoadingDialog() {
if (bar != null) {
bar.dismiss();
bar = null;
}
}
無法在AsynTask中顯示對話框的問題可能是您的if(bar == null){條件,因為那時您的bar對象不會為null。 因此,在關閉對話框時,必須將其初始化為null 。 並且請編寫bar.show(); if(...)條件中的這一行代碼。
正如Preetika Kaur建議的那樣,您應該將Context
對象傳遞給showLoadingDialog()
並調用bar = new ProgressDialog(yourContextObject);
否則,bar將始終為null
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.