读取json时,Android计时器未在progressbar中更新
这是我的代码。我无法在读取json时,Android计时器未在progressbar中更新,android,async-onprogressupdate,Android,Async Onprogressupdate,这是我的代码。我无法在progressbar中更新计时器: class IPListAsy extends AsyncTask<String, Integer, Integer>{ ProgressDialog progressDialog; @Override protected void onPreExecute() { progressDialog = ProgressDialog.sho
progressbar
中更新计时器:
class IPListAsy extends AsyncTask<String, Integer, Integer>{
ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(CurrentHealthActivity.this, "Wait", "getting...");
this.progressDialog.setIndeterminate(false);
this.progressDialog.setMax(100);
this.progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
this.progressDialog.setCancelable(false);
super.onPreExecute();
}
@Override
protected Integer doInBackground(String... params) {
arrayList=service.getIPList(params[0], params[1]);
System.out.println("1-result"+arrayList.size());
return arrayList.size();
}
@Override
protected void onProgressUpdate(Integer... values) {
int current = values[0];
int total = values[1];
System.out.println("value="+values.toString());
float percentage = 100 * (float) current / (float) total;
progressDialog.setProgress((int) percentage);
}
@Override
protected void onPostExecute(Integer result) {
progressDialog.dismiss();
if(result > 0){
System.out.println("3-result"+result);
fillItemListAdapter();
}
else{
Toast.makeText(getApplicationContext(), "No Record Found", Toast.LENGTH_SHORT).show();
}
super.onPostExecute(result);
}
}
类IPListAsy扩展异步任务{
进行对话进行对话;
@凌驾
受保护的void onPreExecute(){
progressDialog=progressDialog.show(CurrentHealthActivity.this,“等待”,“获取…”);
this.progressDialog.setUndeterminate(false);
这个.progressDialog.setMax(100);
this.progressDialog.setProgressStyle(progressDialog.STYLE_水平);
this.progressDialog.setCancelable(false);
super.onPreExecute();
}
@凌驾
受保护的整数doInBackground(字符串…参数){
arrayList=service.getIPList(参数[0],参数[1]);
System.out.println(“1-result”+arrayList.size());
返回arrayList.size();
}
@凌驾
受保护的void onProgressUpdate(整型…值){
int电流=数值[0];
整数总计=数值[1];
System.out.println(“value=“+values.toString());
浮动百分比=100*(浮动)当前/(浮动)总计;
progressDialog.setProgress((int)百分比);
}
@凌驾
受保护的void onPostExecute(整数结果){
progressDialog.disclose();
如果(结果>0){
系统输出打印项次(“3-结果”+结果);
fillItemListAdapter();
}
否则{
Toast.makeText(getApplicationContext(),“未找到记录”,Toast.LENGTH_SHORT.show();
}
super.onPostExecute(结果);
}
}
您必须在doInBackground()
方法中使用publishProgress()
方法来调用onProgressUpdate()
方法
@Override
protected Integer doInBackground(String... params) {
arrayList=service.getIPList(params[0], params[1]);
System.out.println("1-result"+arrayList.size());
int current = arrayList.get(0);
int total = arrayList.get(1);
float percentage = 100 * (float) current / (float) total;
publishProgress((Integer)percentage);
return wateverValueToReturn;
}
@Override
protected void onProgressUpdate(Integer... progress) {
progressDialog.setProgress(progress);
}
还有?问号在哪里?将publishProgress(值)放在doInBackground中并检查