我使用asynctask从Web服务获取数据。但是当线程执行时,progessdialog不显示
private class Task_Bank extends AsyncTask<Void, Void, ArrayList<String>>{
@Override
protected ArrayList<String> doInBackground(Void... params) {
ArrayList<String> arrayBankL = new ArrayList<String>();
String url="http://qhoang.org/webservices/get_list_bank.php";
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = jsonParser.getJSONFromURL(url);
int i = 0;
try{
JSONArray array_bank = jsonObject.getJSONArray("list_bank");
for(i=0;i<array_bank.length();i++){
JSONObject node = array_bank.getJSONObject(i);
JSONObject c = node.getJSONObject("node_list_bank");
String namebank = c.getString("tennganhang");
arrayBankL.add(namebank);
}
}catch(JSONException e){
e.printStackTrace();
}
return arrayBankL;
}
@Override
protected void onPostExecute(ArrayList<String> result) {
ArrayAdapter<String> arrayAdapter_bank = new ArrayAdapter<String>(TaskAActivity.this,android.R.layout.simple_spinner_item,result);
arrayAdapter_bank.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spn_bank.setAdapter(arrayAdapter_bank);
}
@Override
protected void onProgressUpdate(Void... values) {
ProgressDialog MyDialog = ProgressDialog.show( TaskAActivity.this, " " , " Loading. Please wait ... ", true);
}
}
OnCreate()
Task_Bank a = new Task_Bank();
a.execute();
JSONParser是使用来自webservices结果的解析器JSON的类i代码。
答案 0 :(得分:3)
您应该在show()
中onPreExecute()
对话,该对话在任务开始之前启动,并且是正确的位置。在onProgressUpdate
中,您只应增加ProgressDialog
的进度。当任务在onPostExecute()
致电MyDialog.dismiss()
@Override
protected void onPreExecute() {
progressDialog.show();
}
可能是这样的
ProgressDialog MyDialog = ProgressDialog.show(TaskAActivity.this, " " , " Loading. Please wait ... ", true);
// some settings
Task_Bank a = new Task_Bank();
a.execute();
@Override
protected void onPreExecute() {
MyDialog.show();
}
答案 1 :(得分:1)
您应该在代码中进行一些修改以显示进度对话框。
开始在onPreExcecute中显示ProgressDialog。
停止在onPostExcecute中显示进度对话框。
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(context);
dialog.setTitle("Hello");
dialog.setMessage("Please Wait!!!");
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.show();
}
@Override
protected void onPostExecute(Object result) {
dialog.dismiss();
---------
}