1

描述: 我有一個網格視圖,包含橫向模式下的4個項目和縱向模式下的3個項目。網格元素是雜誌縮略圖。點擊雜誌的縮略圖下載開始,出現水平進度條,顯示下載進度。但是當我點擊兩個縮略圖一個接一個時,稍後點擊的縮略圖的進度條只會更新,最後被損壞的雜誌會被下載。 我已經使用雜誌下載的異步任務。如何通過進度條更新實現多個下載?

代碼:

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 


    progress = (ProgressBar) arg1.findViewById(R.id.progress); 
    thumbnail = (ImageView) arg1.findViewById(R.id.thumbnail); 



    thumbnail.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // Download Magazine 
      downloadMagazine.execute(bank.get(index).getPdfLink()); 

      } 
    }); 

} 


class DownloadMagazineTask extends AsyncTask<String, Integer, Long> { 
    File sdDir = Environment.getExternalStorageDirectory(); 

    @Override 
    protected void onPreExecute() { 

     progress.setVisibility(View.VISIBLE); 

    } 

    @Override 
    protected Long doInBackground(String... arg0) { 

     String[] urls = arg0; 

     try { 
      URL url = new URL(urls[0]); 
      URLConnection con = url.openConnection(); 
      int fileLength = con.getContentLength(); 

      InputStream input = new BufferedInputStream(url.openStream()); 

      File file = null; 

      File dir = new File(sdDir + "/BeSpoken/pdfs"); 
      boolean flag = dir.mkdirs(); 
      if (flag) 

       System.out.println("Directory created"); 
      else { 
       System.out.println("Directory not created"); 
       file = new File(dir+ "/"+ bank.get(index).getPdfLink().substring(bank.get(index).getPdfLink().lastIndexOf("/"))); 

      } 
      OutputStream output = new FileOutputStream(file); 

      byte data[] = new byte[1024]; 
      long total = 0; 

      int count; 
      while ((count = input.read(data)) != -1) { 
       total += count; 
       // publishing the progress.... 
       publishProgress((int) (total * 100/fileLength)); 
       output.write(data, 0, count); 
      } 


      String m = bank.get(index).getTitle(); 
      manager.updateDownloadedMagazines(
        Integer.parseInt(m.substring(m.lastIndexOf(" ") + 1)), 
        file.toString()); 
      output.flush(); 
      output.close(); 
      input.close(); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     startActivity(getIntent()); 
     finish(); 

     return null; 
    } 

    @Override 
    protected void onProgressUpdate(Integer... values) { 

     super.onProgressUpdate(values); 
     progress.setProgress(values[0]); 

    } 

    @Override 
    protected void onPostExecute(Long result) { 

     super.onPostExecute(result); 

     progress.setVisibility(View.INVISIBLE); 



    } 

} 

問: 我如何能實現多種下載功能,使進度條顯示每個單獨的下載進度?

回答

0

我已經將圖像視圖和進度條定義爲final,並將這些引用發送到雜誌下載活動正在進行的AsyncTask,點擊圖像視圖。 AsyncTask衍生多個線程,這就是每次下載的進度如何更新。代碼:

@Override 
    public View getView(final int position, View convertView, ViewGroup arg2) { 

     View grid; 

     if (convertView == null) { 
      grid = new View(context); 
      grid = layoutInflater.inflate(item, null); 

     } else { 
      grid = (View) convertView; 
     } 

     final TextView title = (TextView) grid.findViewById(R.id.mgntitle); 
     title.setText(bank.get(position).getTitle()); 
     final ImageView imageView = (ImageView) grid 
       .findViewById(R.id.thumbnail); 
     imageView.setImageResource(R.drawable.icon); 
     final ProgressBar progress = (ProgressBar) grid 
       .findViewById(R.id.progress); 
     final ImageView downloadmark = (ImageView) grid 
       .findViewById(R.id.downloadmark); 
     String pdfLink = bank.get(position).getPdfLink(); 
     String filename = pdfLink.substring(pdfLink.lastIndexOf("/") + 1); 
     final File targetDir = new File(fileLocation + filename); 
     System.out.println("target file name " + targetDir); 

     if (new File(fileLocation + filename).exists()) { 
      if (!getPrefName(filename).equalsIgnoreCase("NA")) { 
       downloadmark.setVisibility(View.VISIBLE); 
      } 
     } 



     imageView.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       if (!targetDir.exists()) { 
         map.put(bank.get(position).getTitle(), progress); 
         new DoBackgroundTask(GridDisplayActivity.this, bank 
           .get(position).getPdfLink(), progress, 
           downloadmark, imageView, position) 
           .execute(); 


       } 


      } 

     }); 

     imageView.setImageBitmap(BitmapFactory.decodeByteArray(
       bank.get(position).getCoverPages(), 0, bank.get(position) 
         .getCoverPages().length)); 


     return grid; 

    }