Android ProgressDialog未在AsyncTask中解除
我有一个应用程序,可以在一个异步任务中从网站上发布一个微调器。如果没有ProgressDialog,我将一直使用空微调器,直到它加载为止。使用ProgressDialog,网络操作永远不会完成,ProgressDialog永远不会停止。这是我的密码:Android ProgressDialog未在AsyncTask中解除,android,android-asynctask,progressdialog,Android,Android Asynctask,Progressdialog,我有一个应用程序,可以在一个异步任务中从网站上发布一个微调器。如果没有ProgressDialog,我将一直使用空微调器,直到它加载为止。使用ProgressDialog,网络操作永远不会完成,ProgressDialog永远不会停止。这是我的密码: class TheaterGetter extends AsyncTask<Void, Void, Document> { private Context context; private ProgressDial
class TheaterGetter extends AsyncTask<Void, Void, Document> {
private Context context;
private ProgressDialog dialog;
public TheaterGetter(Context context) {
this.context = context;
dialog = new ProgressDialog(context);
}
@Override
protected void onPreExecute() {
dialog.setMessage(((Activity) context).getString(R.string.loading_theaters));
dialog.setCancelable(false);
dialog.show();
}
protected Document doInBackground(Void...voids) {
Document doc = null;
try {
doc = Jsoup.connect("http://landmarkcinemas.com").timeout(10000).get();
} catch (IOException e) {
Log.e("landmark cinemas connection error", e.getMessage());
}
return doc;
}
protected void onPostExecute(Document doc) {
Element allOptions = doc.select("select[id=campaign").first();
Elements options = allOptions.getElementsByTag("option");
options.remove(0);
TreeMap<String, String> theaters = new TreeMap<String, String>();
for (Element option:options) {
theaters.put(option.html(), option.attr("value"));
}
final TreeMapSpinAdapter adapter = new TreeMapSpinAdapter(context, android.R.layout.simple_spinner_item, theaters);
final Spinner spinner = (Spinner) ((Activity) context).findViewById(R.id.spinner1);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
MainActivity.setTheater((String) adapter.getItem(pos));
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
dialog.dismiss();
}
}
class theategetter扩展异步任务{
私人语境;
私人对话;
公共戏剧演员(背景){
this.context=上下文;
dialog=新建进度对话框(上下文);
}
@凌驾
受保护的void onPreExecute(){
setMessage(((活动)上下文).getString(R.string.loading_theates));
对话框。可设置可取消(false);
dialog.show();
}
受保护文档背景(作废…作废){
单据单据=空;
试一试{
doc=Jsoup.connect(“http://landmarkcinemas.com)超时(10000).get();
}捕获(IOE异常){
Log.e(“landmark cinemas连接错误”,e.getMessage());
}
退货单;
}
后期执行时受保护的无效(文档文档){
元素allOptions=doc.select(“选择[id=campaign”).first();
Elements options=allOptions.getElementsByTag(“选项”);
选项。删除(0);
TreeMap剧院=新的TreeMap();
用于(元素选项:选项){
theaters.put(option.html(),option.attr(“value”);
}
最终TreeMapSpinAdapter适配器=新的TreeMapSpinAdapter(上下文,android.R.layout.simple\u微调器\u项,剧院);
最终微调器微调器=(微调器)((活动)上下文);
旋转器。设置适配器(适配器);
spinner.setOnItemSelectedListener(新的OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图、,
内部位置,长id){
MainActivity.setTheater((字符串)adapter.getItem(pos));
}
@凌驾
未选择公共无效(AdapterView arg0){
//TODO自动生成的方法存根
}
});
dialog.dismise();
}
}
只需按如下方式重新排列“取消”对话框行:
protected void onPostExecute(Document doc) {
dialog.dismiss(); // rearranged
Element allOptions = doc.select("select[id=campaign").first();
Elements options = allOptions.getElementsByTag("option");
options.remove(0);
TreeMap<String, String> theaters = new TreeMap<String, String>();
for (Element option:options) {
theaters.put(option.html(), option.attr("value"));
}
final TreeMapSpinAdapter adapter = new TreeMapSpinAdapter(context, android.R.layout.simple_spinner_item, theaters);
final Spinner spinner = (Spinner) ((Activity) context).findViewById(R.id.spinner1);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
MainActivity.setTheater((String) adapter.getItem(pos));
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
后期执行时受保护的作废(文档文档){
dialog.dismise();//重新排列
元素allOptions=doc.select(“选择[id=campaign”).first();
Elements options=allOptions.getElementsByTag(“选项”);
选项。删除(0);
TreeMap剧院=新的TreeMap();
用于(元素选项:选项){
theaters.put(option.html(),option.attr(“value”);
}
最终TreeMapSpinAdapter适配器=新的TreeMapSpinAdapter(上下文,android.R.layout.simple\u微调器\u项,剧院);
最终微调器微调器=(微调器)((活动)上下文);
旋转器。设置适配器(适配器);
spinner.setOnItemSelectedListener(新的OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图、,
内部位置,长id){
MainActivity.setTheater((字符串)adapter.getItem(pos));
}
@凌驾
未选择公共无效(AdapterView arg0){
//TODO自动生成的方法存根
}
});
}
我认为这与您的问题无关,但您应该在doInBackground
中解析文档,并返回结果剧院
,以便onPostExecute
可以在UI上显示它们。解析可能需要一些时间才能运行,因此您应该避免在UI线程上执行。哦,出于某种原因,我认为这可能是g我很想节约使用doInBackground,但我明白你的意思。