这个网站已经帮助我一年了,这是我第一次找不到问题的答案。
我正在尝试从AsyncTask 发布进度,而没有ProgressDialog 。用户刚刚输入了一个地址,我想在地图和列表中显示距该地址一定半径范围内的名胜古迹。因此,我需要从数据库中获取这些地点的地址并计算距离,然后才能知道需要在地图上显示哪些地点作为标记。
在计算距离时(在AsyncTask中),我希望在标记最终显示在地图上之前让用户了解进度。
我的代码存在两个主要问题:
如果有任何暗示或建议,请先感谢!
我的AsyncTask看起来像这样:
public class GetDistanceTask extends AsyncTask<Void,Integer,Integer>
{
private Location mSearchAddress;
private FragmentManager mSfm;
private ContentResolver mCr;
private static final String TAG = GetDistanceTask.class.getSimpleName();
private DialogFragment mDf;
private ProgressDialog mProgressDialog;
public GetDistanceTask(FragmentManager sfm, ContentResolver cr, Location searchAddress){
mSfm = sfm;
mCr = cr;
mSearchAddress = searchAddress;
}
@Override
protected Integer doInBackground(Void...voids) {
int rowsUpdated = 0;
int max = 0;
final Uri locationUri = KitaContract.LocationEntry.CONTENT_URI;
String selection = KitaProvider.sLocationKitaSelection;
String[] args = new String[] {"3"};
//All entries, unsorted
Cursor cursor = mCr.query(locationUri, null, selection, args, null);
if (cursor != null) {
max = cursor.getCount();
cursor.moveToFirst();
} else {
Log.e(TAG, "Cursor == null !");
return 0;
}
do{
double kitaLat = cursor.getDouble(COL_LAT);
double kitaLong = cursor.getDouble(COL_LONG);
int id = cursor.getInt(COL_LOCID);
//make a Location-object from Kita's Lat & Long
Location kitaAddress = new Location("kitaName");
kitaAddress.setLatitude(kitaLat);
kitaAddress.setLongitude(kitaLong);
//calculate distance
float distance = kitaAddress.distanceTo(mSearchAddress);
ContentValues contentValues = new ContentValues();
contentValues.put(KitaContract.LocationEntry.COLUMN_DIST, distance);
//add the distance to the kita entry in the database
int rowUpdated =mCr.update(
locationUri,
contentValues,
KitaContract.LocationEntry._ID + " = ?",
new String[] {String.valueOf(id)});
rowsUpdated += rowUpdated;
publishProgress(rowsUpdated, max);
}while (cursor.moveToNext());
cursor.close();
return rowsUpdated;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// instantiate ProgressDialog
mDf = (DistanceProgressDialogFragment) mSfm.findFragmentByTag("dpdf_tag");
if (mDf == null){
mDf = new DistanceProgressDialogFragment();
mSfm.beginTransaction()
.add(mDf, "dpdf_tag")
.commitAllowingStateLoss();
}
else mDf.show(mSfm, "dpdf_tag");
if (mDf != null) {
mProgressDialog = (ProgressDialog) mDf.getDialog();
mProgressDialog.setProgressNumberFormat("%1d/%2d Kitas");
}
}
@Override
protected void onProgressUpdate(Integer... progress) {
mProgressDialog.setMax(progress[1]);
mProgressDialog.setProgress(progress[0]);
super.onProgressUpdate(progress);
}
@Override
protected void onPostExecute(Integer rowsUpdated) {
if ( mDf != null) {
mDf.dismiss();
}
}
我的DialogFragment是这样的:
public class DistanceProgressDialogFragment extends DialogFragment
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setCancelable(false);
}
@NonNull
@Override
public ProgressDialog onCreateDialog(Bundle savedInstanceState)
{
ProgressDialog dialog = new ProgressDialog(getActivity(), getTheme());
dialog.setTitle("Kita Guide");
dialog.setMessage("Suche Kitas in der Nähe");
dialog.setIndeterminate(true);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
return dialog;
}
}
答案 0 :(得分:0)
dialog.setIndeterminate(true);
表示进度条应该永远运行。