ProgressDialog在ActionBarSherlock中不会更新

问题描述 投票:0回答:1

我在进度对话框中看不到任何进度更新。

在活动班级:

void retrieveImages()
{
        mImageUriList = new ArrayList<String>();
        mImageUriList = Utils.fetchImages(this);
        mProgressDialog = new ProgressDialog(this);

        // progress dialog here is class level activity member
        mProgressDialog.show();
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.setMessage("Processing Images..");
        mProgressDialog.setMax(100);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        mProgressDialog.setCanceledOnTouchOutside(false);
        new GetImageAsyncTask(this, mImageUriList,mProgressDialog).execute();

        // passing progress dialog to async task
    }

在AsyncTask中

@Override
    protected Void doInBackground(Void... params) 
    {
    int progress_status;
    float count=imageUriList.size();
    float increment=0;
    float p=(increment/count)*100;
        for(int i=0;i<count;i++)
        {
            increment++;
            //do some operation
            p=(increment/count)*100;
            progress_status=Math.round(p);
            publishProgress(progress_status);
            Log.i(TAG, "progress value-"+progress_status);
        }   
        return null;
    }
    @Override
protected void onProgressUpdate(Integer... values) 
{
    //super.onProgressUpdate(values);
    progressDialog.setProgress(values[0]);
}

我只能看到微调框和消息“正在处理图像。。,我看不到任何进度条/更新

我正在使用ActionBarSherLock,我也尝试了此解决方案here,该解决方案似乎也不起作用,它也只是在操作栏上显示进度微调器。我希望对话框显示进度,而不是在操作栏上。

java android actionbarsherlock progressdialog progress
1个回答
0
投票

这里的技巧不是在活动中创建进度条,通过将Activity的上下文传递到AsyncTask中在AsyncTask中创建进度对话框,在这里查看代码

void retrieveImages()
{
        //some method in sherlock activity 
        mImageUriList = new ArrayList<String>();
        mImageUriList = Utils.fetchImages(this);
        new GetImageAsyncTask(this, mImageUriList).execute();
        //NOT passing progress dialog to async task,just activity context
}

在AsyncTask中

在构造函数中

GetImageAsyncTask(Context mContext,ArrayList<String> mImageUriList)
{
    // Other intializations
    mProgressDialog = new ProgressDialog(mContext);

    mProgressDialog = new ProgressDialog(this);
    // progress dialog here is asynctask class member
    mProgressDialog.setIndeterminate(false);
    mProgressDialog.setMessage("Processing Images..");
    mProgressDialog.setMax(100);
    mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    mProgressDialog.setCanceledOnTouchOutside(false);
}

现在onPreExecute(),显示/加载进度对话框

public void onPreExecute()
{
   //show dialog box on pre execute
   mProgressDialog.show();
}

保持doInBackgroundonProgressUpdate相同

© www.soinside.com 2019 - 2024. All rights reserved.