从 AlertDialog 执行 AsyncTask 时出现奇怪的行为。我需要一些建议/解决方法来解决它。我被困在这一点上。
当我从 AlertDialog 执行 AsyncTask 时,它没有调用 onPostExecute。它调用doInBackground,但完成后,它不调用onPostExecute。我希望 AsyncTask 根据 AlertDialog 的按钮按下值执行。
下面是创建 AlertDialog 并执行 AsyncTask 的函数:
private void processDownloadChoosen(String msg, int __position){
final AlertDialog.Builder alertBox = new AlertDialog.Builder(new ContextThemeWrapper(ShivaniMP3Activity.this, android.R.style.Theme_Dialog));
final int position = __position;
alertBox.setMessage(msg);
alertBox.setCancelable(false)
.setPositiveButton("Download", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int id){
dialog.dismiss();
String downloadURL = entries.get(position);
AsyncTaskDownload atd = new AsyncTaskDownload(downloadURL);
if((downloadURL != null) &&(downloadURL != "")){
EnglishMP3Activity.totalDownloads++;
if(downloadWindow == null){
downloadWindow = new PopupWindow(downloadPopupLayout, LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT, true);
downloadWindow.showAtLocation(downloadPopupLayout, Gravity.CENTER, 0, 0);
}
atd.execute();
}
}
}).setNegativeButton("Listen", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int id){
dialog.dismiss();
String downloadURL = entries.get(position).replace("%20", " ");
emp = new EasyMediaPlayer(mp3PopupLayout,buttonPlayPause,seekBarProgress,tv_mp3,downloadURL);
emp.startPlayingMP3();
}
}).show();
}
我从listview的项目点击调用这个函数:
//lv is listview
lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView<?> p, View v, int position, long id) {
processDownloadChoosen("Do you want to Listen or Download this file ?",position);
}
});
我的 AsyncTask 定义如下所示:
public class AsyncTaskDownload extends AsyncTask<Void, String, Void> {
//
protected void onPreExecute(){
pBar1.setVisibility(View.INVISIBLE);
//
}
protected Void doInBackground(Void... vd){
try{
//do something
}
catch(Exception e){
//do somting
}
return null;
}
protected void onProgressUpdate(String... msg) {
//do smething
}
protected void onPostExecute(Void in){
cancelDownloadButton.setVisibility(View.GONE);
//do smtthing
}
}
请注意:当我直接从 ListView 的项目单击功能执行 AsyncTask 时,一切正常。但是从 AlertDialog 调用时,它不会调用 onPostExecute。
对此问题的任何帮助/解决方法表示赞赏..提前谢谢