首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在不使用ProgressDialog的情况下使用DialogFragment从AsyncTask显示进度

如何在不使用ProgressDialog的情况下使用DialogFragment从AsyncTask显示进度
EN

Stack Overflow用户
提问于 2018-08-12 01:52:18
回答 1查看 37关注 0票数 0

这个网站已经帮了我一年多了,这是我第一次找不到问题的答案。

我正在尝试在没有ProgressDialog的AsyncTask 中发布进度。用户只需输入一个地址,我希望在地图和列表中显示该地址一定半径范围内的名胜。因此,我需要从数据库中获取这些地方的地址并计算距离,然后才能知道需要在地图上显示哪些地方作为标记。

在计算距离(在AsyncTask中)时,我希望在标记最终显示在地图上之前,让用户了解最新的进度。

我的代码有两个主要问题:

  • It将正确显示并关闭DialogFragment,但不会显示进度(无论是按条还是按numbers)
  • I都不想使用ProgressDialog

提前感谢您的任何提示或建议!

我的AsyncTask看起来像这样:

代码语言:javascript
运行
复制
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是这样的:

代码语言:javascript
运行
复制
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;
}
}
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51802317

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档