我有一个包含AsyncTask类的对话框片段。我能够在dialogfragment中显示进度条。但是一段时间后无法阻止它。有什么办法吗?
class GetStatsFromUrl extends AsyncTask<String, String, String> {
//ProgressDialogFragment pf;
ProgressBar p = null;
String info, status;
JSONObject json, json1, json2;
/*String totalquestions, notanswered,
correctanswered, wronganswered;*/
String totalquestions, notanswered,
correctanswered, wronganswered;
@SuppressLint("InlinedApi")
@Override
protected void onPreExecute() {
super.onPreExecute();
//p = new ProgressDialog(getActivity());
//p.setMessage("Loading..");
//p.setIndeterminate(true);
// p.setCancelable(false);
//p.show();
p=new ProgressBar(getActivity());
// p.setVisibility(View.VISIBLE);
//p = (ProgressBar) rootView.findViewById(R.id.loading_spinner);
//p.setVisibility(View.VISIBLE);
// p.setMax(3000 -1);
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
// try {
String url1 = "http://smarteach.com/questions/questions.svc/Learner_Qbank_Stats_Today/val1="
+ learnerid
+ "/val2="
+ courseid
+ "/val3="
+ session_id + "";
System.out.println("Stats from URL : " + url1);
ServiceHandler sh = new ServiceHandler();
jsonstring = sh.makeServiceCall(url1, ServiceHandler.GET);
System.out.println("Response: " + jsonstring);
if (jsonstring != null && jsonstring.length() > 0) {
try {
JSONObject questionsObject = new JSONObject(jsonstring);
JSONArray questionsArray = questionsObject
.getJSONArray("Table");
if (questionsArray != null && questionsArray.length() > 0) {
for (int i = 0; i < questionsArray.length(); i++) {
JSONObject innerQuestionObject = (JSONObject) questionsArray
.get(i);
String count = innerQuestionObject.getString("Count");
String result = innerQuestionObject.getString("Result");
if (result.equalsIgnoreCase("R")) {
correctanswered = count;
} else if (result.equalsIgnoreCase("Total Questions")) {
totalquestions = count;
} else if(result.equalsIgnoreCase("W")){
// notanswered = count;
wronganswered = count;
}
/* if(result.equalsIgnoreCase("W")){
wronganswered = count;
}*/
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}else {
Toast.makeText(getActivity(), "Please try after some time",
Toast.LENGTH_LONG).show();
}
return url1;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
//p.dismiss();
// TODO Auto-generated method stub
System.out.println("In onpostexecute status");
// p.setVisibility(View.GONE);
tvbookmarkcount.setText(totalquestions);
//tvquesunattempted.setText(notanswered);
tvcorrectanswered.setText(correctanswered);
tvwronganswered.setText(wronganswered);
}
}
答案 0 :(得分:1)
尝试以下代码: -
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
p.dismiss();
}
你必须在任务完成时调用dismiss(onPostExecute)。