2011-08-17 27 views
6

我有一個奇怪的問題,我有launced每當我發現在web瀏覽器的MP3文件的新的AsyncTask,並勞克在ListView每個的AsyncTask的進度條。所以下載次數可以超過1次,同時進行。但現在只要是的AsyncTask進度launced同一移動所有,而不是不同的不同的AsyncTask,PLZ指導我.......列表視圖中的進度條,同時的AsyncTask文件下載

LISTVIEW WITH PROGRESS BAR

public class CopyOfDownloadsListActivity extends ListActivity { 
    /** Called when the activity is first created. */ 

// static ArrayList<String> pthreads = new ArrayList<String>(); 
ImageView bt; 
ProgressBar pb; 
ListView allList; 
TextView tv; 
String fileName; 
String mp3URL; 
    URL url2; 
    int filecount = 0; 

private class DownloadFile extends AsyncTask<String, Integer, Void>{ 
    MyCustomAdapter adapter; 

    int count = 0; 
    ProgressDialog dialog; 
    ProgressBar progressBar; 
    int myProgress; 

    @Override   
    protected Void doInBackground(String... u) {   
     try {     
      URL ul = new URL(u[0]); 
      Log.i("UI",ul.toString()); 
     // int len = CopyOfMusicDownloader.mp3urls.size(); 
     // URL url2 = new URL(CopyOfMusicDownloader.mp3urls.get(len-1)); 
      HttpURLConnection c = (HttpURLConnection) ul.openConnection(); 
      c.setRequestMethod("GET"); 
      c.setDoOutput(true); 
      c.connect(); 

      int lengthOfFile = c.getContentLength(); 

      String PATH = Environment.getExternalStorageDirectory() 
        + "/download/"; 
      Log.v("", "PATH: " + PATH); 
      File file = new File(PATH); 
      file.mkdirs(); 

      fileName = "Track"; 
      filecount++; 

      fileName = fileName + Integer.toString(filecount) + ".mp3"; 


      File outputFile = new File(file, fileName); 
      FileOutputStream fos = new FileOutputStream(outputFile); 
      InputStream is = c.getInputStream(); 

      byte[] buffer = new byte[1024]; 
      int len1 = 0;  
      while ((len1 = is.read(buffer)) != -1) { 
       myProgress = (int)((len1/lengthOfFile)*100); 
       myProgress = myProgress + myProgress; 
       Log.i("lengthOfFile", Integer.toString(lengthOfFile)); 
       Log.i("My Progress", Integer.toString(myProgress)); 
       publishProgress(myProgress); 
       fos.write(buffer, 0, len1); 
      } 
      fos.close(); 
      is.close(); 

      }catch (IOException e) { 
        e.printStackTrace(); 
      } 
     return null; 
    } 

    protected void onPostExecute() { 
    } 

    @Override 
    protected void onPreExecute() {   
      adapter = new MyCustomAdapter(CopyOfDownloadsListActivity.this, R.layout.row, CopyOfMusicDownloader.mp3urls); 
      setListAdapter(adapter); 
    } 



    @Override 
    protected void onProgressUpdate(Integer... values) { 
     Log.i("Value", values[0].toString()); 
     count++; 
     adapter.notifyDataSetChanged(); 
    } 


    public class MyCustomAdapter extends ArrayAdapter<String> {  
     public MyCustomAdapter(Context context, int textViewResourceId, ArrayList<String> pthreads) { 
     super(context, textViewResourceId, pthreads); 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
       LayoutInflater inflater = getLayoutInflater(); 
       View row = inflater.inflate(R.layout.row, parent, false); 
       bt =(ImageView)row.findViewById(R.id.cancel_btn); 
       tv =(TextView)row.findViewById(R.id.filetext); 
       pb = (ProgressBar)row.findViewById(R.id.progressbar_Horizontal); 
       pb.setProgress(count); 
       return row; 
      } 
     }  
} 

這是開始的AsyncTask,它的onCreate new DownloadFile()。execute(url2.toString());

回答

3

你的問題是這個static ProgressBar pb;

你不能有一個單一的靜態參考,並期望控制多個進度條。將ProgressBar完全封裝在AsyncTask中,使其成爲實例變量。

編輯

在onProgressUpdate您需要更改進度的進度。每行都不需要適配器,這是浪費的。

@Override 
    protected void onProgressUpdate(Integer... values) { 
     Log.i("Value", values[0].toString()); 
     count++; 
     progressBar.setProgress(count); 
    } 

你也從來沒有賦值進度,你繼續評估pb,擺脫變量!在您PreExecute中,您將繼續重新分配List的適配器。

你需要修改了很多。創建一個管理列表視圖的適配器。每行可以有一個AsyncTask來維護自己的進度條,圖像視圖和文本視圖。

+0

謝謝我刪除它,但問題仍然存在....... :( – Programmer

4

此外,閱讀上執行的說明()在這裏:

http://developer.android.com/reference/android/os/AsyncTask.html#execute%28Params...%29

蜂窩後會有運行的所有ASyncTasks所以你可能不能同時運行一個以上的只有一個線程。在第一個之後,其餘的將排隊等待,直到第一個完成後纔會執行。您可能需要在一個線程中執行每個下載並使用一個ASyncTask來監視所有這些。 (你可以從一個線程監控太多,但你需要採取額外措施來安全地張貼在UI線程上更新到UI)。

+0

感謝您指出,我不知道他們改變它回到串行池默認 – smith324

+0

np - 只是瞭解到它自己的硬盤的方式:) – Fraser

+1

難的方法?文件說它還沒有改變。 – smith324