2013-08-12 90 views
0

我在擴展AsyncTask的類中實現ProgressDialog時遇到問題。Implementing ProgressDialog AsyncTask

我有以下問題:

super.onProgressUpdate(進度);從蝕

消息:在類型的AsyncTask方法onProgessUpdate(空隙...)是不適用的參數(整數[])

任何解決方案?

static class BitmapWorkerTask extends AsyncTask <int[], Void, Bitmap[]> { 

     private int[] data; 
     private int width, height; 
     private int screenWidth; 
     private int screenHeight; 
     private int nmbrOfImages; 
     private int[] imgRes, textRes; 
     private String[] scrollText; 
     private ImageView[] imageView; 
     private TextView[] textView; 
     private View view; 
     private LayoutInflater factory; 
     private AlertDialog.Builder alertadd; 
     private Context context; 
     private WeakReference <Context> sc; 
     private WeakReference <Bitmap[]> bitmapV; 


     public BitmapWorkerTask(int nmbrOfImages, String[] scrollText, Context context) { 

      this.nmbrOfImages = nmbrOfImages; 
      this.scrollText = scrollText; 
      this.context = context; 

      view = null; 
      factory = null; 
      alertadd = null; 
      System.gc(); 

      sc = new WeakReference <Context> (context); 

      try { 
       for (int i = 0; i < scaledBitmap.length; i++) { 
        scaledBitmap[i].recycle(); 
        scaledBitmap[i] = null; 
       } 
      } catch (NullPointerException ne) { 
       System.out.println("nullpointerexception ... gick inte recycla bitmapbilder"); 
      } 

      data = new int[nmbrOfImages]; 
      imageView = new ImageView[nmbrOfImages]; 
      textView = new TextView[nmbrOfImages]; 
     } 

     @Override 
     protected Bitmap[] doInBackground(int[] ... params) { 
      data = params[0]; 

      alertadd = new AlertDialog.Builder(sc.get()); 
      factory = LayoutInflater.from(sc.get()); 
      // Ta reda på skärmens dimensioner. 
      DisplayMetrics metrics = context.getResources().getDisplayMetrics(); // Beräkna skärmens dimensioner. 
      screenWidth = metrics.widthPixels; 
      screenHeight = metrics.heightPixels; 
      width = ((int)(this.screenWidth * 1)); 
      height = (int)(width * 1.5); 
      Bitmap[] bm = decodeSampledBitmapFromResource(sc.get().getResources(), data, width, height); 
      bitmapV = new WeakReference <Bitmap[]> (bm); 

      view = factory.inflate(R.layout.scrollview, null); 

      imgRes = new int[] {R.id.img1, R.id.img2, R.id.img3, R.id.img4, R.id.img5, R.id.img6, R.id.img7, R.id.img8, R.id.img9}; 
      textRes = new int[] {R.id.text_img1, R.id.text_img2, R.id.text_img3, R.id.text_img4, R.id.text_img5, R.id.text_img6, 
      R.id.text_img7, R.id.text_img8, R.id.text_img9}; 

      for (int i = 0; i < nmbrOfImages; i ++) { 
       imageView[i] = (ImageView) view.findViewById(imgRes[i]); 
       textView[i] = (TextView) view.findViewById(textRes[i]); 
      } 

      return bitmapV.get(); 
     } 

     protected void onPostExecute(Bitmap[] bitmap) { 
      for (int i = 0; i < nmbrOfImages; i++) { 
       imageView[i].setImageBitmap(bitmap[i]); 

       textView[i].setText(Html.fromHtml(scrollText[i])); 

       textView[i].setVisibility(TextView.VISIBLE); 
      } 
      loadAlertDialog(); 
     } 

     /** 
     * Updating progress bar 
     * */ 
     protected void onProgressUpdate(Integer ... progress) { 
      // setting progress percentage 

      super.onProgressUpdate(progress); 
      progressBar.setProgress(0); 
      progressBar.setProgress(progress[0]); 
     } 


     private void startProgressBar() { 
      progressBar = new ProgressDialog(context); 
      progressBar.setCancelable(true); 
      progressBar.setMessage("File downloading ..."); 
      progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
      progressBar.setProgress(0); 
      progressBar.setMax(100); 
      progressBar.show(); 
     } 
} 

回答

0

更改你的異步任務到:

static class BitmapWorkerTask extends AsyncTask <int[], Integer, Bitmap[]> 
+0

你不能pa ss''''''''數據類型'int []'作爲'AsyncTask'中的一個參數。你需要傳遞'wrapper'數據類型。將其更改爲靜態類BitmapWorkerTask擴展AsyncTask ' –

+0

@Kaidul Islam Sazal:在實現ProgressDialog之前,我沒有問題將數據類型int []作爲參數傳遞給AsyncTask。我遵循納爾吉斯的例子,我現在看到的是錯誤信息消失了。 –

0

改變你的類初始化這個

靜態類BitmapWorkerTask擴展的AsyncTask {

1

更改fiesr行此

static class BitmapWorkerTask extends AsyncTask <Integer[], Void, Bitmap[]>