【问题标题】:Progress bar inside a non asynctask class in androidandroid中非异步任务类中的进度条
【发布时间】:2013-09-30 01:23:53
【问题描述】:

是否可以在不扩展异步任务的类中使用或显示进度条?我正在尝试创建一个图片库,当它加载所有图片时,我想在用户等待图片库出现时显示进度条。

下面的代码是我试图在后台加载的:

final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
        final String orderBy = MediaStore.Images.Media._ID;
        Cursor imagecursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,null, orderBy + " DESC");

        int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
        this.count = imagecursor.getCount();
        this.thumbnails = new Bitmap[this.count];
        this.arrPath = new String[this.count];
        this.thumbnailsselection = new boolean[this.count];
        for (int i = 0; i < this.count; i++) {
            imagecursor.moveToPosition(i);
            int id = imagecursor.getInt(image_column_index);
            int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
            thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(getApplicationContext().getContentResolver(), id,
                    MediaStore.Images.Thumbnails.MICRO_KIND, null);
            arrPath[i]= imagecursor.getString(dataColumnIndex);
        }

        final GridView imagegrid = (GridView) findViewById(R.id.grid_GalleryImage);
        imageAdapter = new ImageAdapter(this, false);
        imagegrid.setAdapter(imageAdapter);

得到错误:

09-30 10:08:00.560: W/System.err(9283): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
09-30 10:08:00.565: W/System.err(9283):     at android.view.ViewRoot.checkThread(ViewRoot.java:3518)
09-30 10:08:00.565: W/System.err(9283):     at android.view.ViewRoot.invalidateChild(ViewRoot.java:604)
09-30 10:08:00.565: W/System.err(9283):     at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:652)
09-30 10:08:00.565: W/System.err(9283):     at android.view.ViewGroup.invalidateChild(ViewGroup.java:3693)
09-30 10:08:00.565: W/System.err(9283):     at android.view.View.invalidate(View.java:7228)
09-30 10:08:00.565: W/System.err(9283):     at android.view.View.invalidate(View.java:7183)
09-30 10:08:00.565: W/System.err(9283):     at android.widget.AbsListView.resetList(AbsListView.java:1749)
09-30 10:08:00.565: W/System.err(9283):     at android.widget.GridView.setAdapter(GridView.java:199)
09-30 10:08:00.565: W/System.err(9283):     at com.fdf.ireport.S_6th_ImageGallery.doWork(S_6th_ImageGallery.java:245)
09-30 10:08:00.565: W/System.err(9283):     at com.fdf.ireport.S_6th_ImageGallery$1.run(S_6th_ImageGallery.java:96)
09-30 10:08:00.565: W/System.err(9283):     at java.lang.Thread.run(Thread.java:1020)
09-30 10:08:00.565: W/dalvikvm(9283): threadid=9: thread exiting with uncaught exception (group=0x4026d760)

【问题讨论】:

    标签: android android-asynctask progress-bar


    【解决方案1】:

    是的,通过使用ProgressBar,您可以创建ProgressBar,而无需使用Asynctask

    示例如下:

    public class MyActivity extends Activity {
         private static final int PROGRESS = 0x1;
    
         private ProgressBar mProgress;
         private int mProgressStatus = 0;
    
         private Handler mHandler = new Handler();
    
         protected void onCreate(Bundle icicle) {
             super.onCreate(icicle);
    
             setContentView(R.layout.progressbar_activity);
    
             mProgress = (ProgressBar) findViewById(R.id.progress_bar);
    
             // Start lengthy operation in a background thread
             new Thread(new Runnable() {
                 public void run() {
                     while (mProgressStatus < 100) {
                         mProgressStatus = doWork();
    
                         // Update the progress bar
                         mHandler.post(new Runnable() {
                             public void run() {
                                 mProgress.setProgress(mProgressStatus);
                             }
                         });
                     }
                 }
             }).start();
         }
     }
    

    来源:Here

    【讨论】:

    • doWork(); 正在加载我想在用户等待画廊出现时显示进度条的所有图像返回int 值并表示百分比的方法ProgressBar状态
    • 我已经更新了上面的代码。当我要使用它时,我应该返回什么值?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-16
    • 1970-01-01
    • 2018-12-26
    • 1970-01-01
    相关资源
    最近更新 更多