0
更新對話框我有一個進步的PreferenceFragment內部對話框,並且不更新其進度:Android的進步,從不會的AsyncTask
進度對話框下面的監聽器裏初始化
@Override
public boolean onPreferenceClick(Preference preference) {
if (getString(R.string.sync).equalsIgnoreCase(preference.getKey())) {
String saveTitle = getString(R.string.crop__wait);
String saveDescription = getString(R.string.sync_is_running);
progressDialog = ProgressDialog.show(getActivity(), saveTitle, saveDescription, false, true);
progressDialog.setProgress(10);
progressDialog.setMax(100);
progressDialog.show();
new SyncAsyncTask(this).execute();
return true;
} else if (getString(R.string.changePassword).equalsIgnoreCase(preference.getKey())) {
ChangePasswordDialog.createDialog(getString(R.string.changePassword)).show(getActivity().getSupportFragmentManager(), "YES_NO_DIALOG");
return true;
}
return true;
}
在這裏,我我正在更新進度對話框,但是ui沒有出現任何進展。僅顯示ProgressDialog的以下部分:標題,描述和微調器。
@Override
public void updateProgress(int progress) {
total += progress;
progressDialog.setProgress(total);
progressDialog.incrementProgressBy(total);
}
奇怪的事實是,這個代碼使可見的進展:
progressDialog = new ProgressDialog (getActivity());
progressDialog.setIndeterminate(false);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setCancelable(true);
progressDialog.setTitle(saveTitle);
progressDialog.setMessage(saveDescription);
progressDialog.setMax(100);
progressDialog.show();
我也張貼了異步任務的代碼,但我相信這是罰款:
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
import java.lang.ref.WeakReference;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class SyncAsyncTask extends AsyncTask<Void, Integer, Boolean> {
private WeakReference<ISyncronizable> ref = null;
private final int totalCount = 21;
public SyncAsyncTask(ISyncronizable syncronizable) {
ref = new WeakReference<>(syncronizable);
}
@Override
protected Boolean doInBackground(Void... params) {
if (ref.get().getContext() != null) {
return synchronizeDB(ref.get().getContext());
}
return false;
}
@Override
protected void onPostExecute(Boolean result) {
if (ref.get().getContext() != null) {
ref.get().finishedSyncronization(result);
ref.clear();
}
}
@Override
protected void onProgressUpdate(Integer... values) {
ref.get().updateProgress(values[0]);
}
public boolean synchronizeDB(final Context context) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2000);
cal.set(Calendar.MONTH, 1);
cal.set(Calendar.DAY_OF_MONTH, 1);
SoapSyncService ss = SoapSyncService.getSoapSyncService();
boolean successfullSync = true;
SystemSettingBean systemSettingsBean = SQLMceHelper.getDb(context).getLatestSyncDate();
Date date = cal.getTime();
String serverDateAndTime = ss.getServerDateAndTime();
if (systemSettingsBean == null || systemSettingsBean.getDateValue() == null) {
systemSettingsBean = new SystemSettingBean();
systemSettingsBean.setDateValue(serverDateAndTime);
date = cal.getTime();
} else {
//........
if (numberOfChanges == 0) {
return true;
}
}
publishProgress(1);
//........
publishProgress(5);
//........
publishProgress(4);
//........
publishProgress(4);
//........
publishProgress(4);
//........
publishProgress(4);
//........
publishProgress(4);
//........
publishProgress(4);
//........
publishProgress(5);
//........
publishProgress(5);
//........
publishProgress(5);
//........
// //........
publishProgress(5);
//........
publishProgress(5);
//........
publishProgress(5);
//........
publishProgress(5);
//........
publishProgress(5);
//........
publishProgress(5);
//........
publishProgress(5);
//........
publishProgress(5);
//........
publishProgress(5);
//........
publishProgress(5);
//........
if (successfullSync) {
//........
}
publishProgress(5);
return successfullSync;
}
}
而且接口代碼:
import android.content.Context;
public interface ISyncronizable {
public Context getContext();
public void updateProgress(int progress);
public void finishedSyncronization(boolean isSuccessfullyFinished);
}
奇怪的是,使用此代碼的進步是明顯的:
progressDialog = new ProgressDialog (getActivity());
progressDialog.setIndeterminate(false);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setCancelable(true);
progressDialog.setTitle(saveTitle);
progressDialog.setMessage(saveDescription);
progressDialog.setMax(100);
progressDialog.show();
更新與片段,其中的進步正在更新你的問題。你是否嘗試過記錄'int progress'的值? – Sufian
這部分是好的,因爲我在應用程序的兩面使用了進度。在PrefFragment(上面的代碼)和LoginFragment中調用處理進度的相同接口。對於登錄片段的進展正常,但我使用TextView來顯示進度。 – aurelianr
可以請你出示你的SyncAsyncTask源碼 –