android中非asynctask类内的进度条

时间:2013-09-30 01:23:53

标签: android android-asynctask progress-bar

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

下面的代码就是我想在后台加载的代码:

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)

1 个答案:

答案 0 :(得分:0)

是的,使用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