2011-08-17 17 views
6

私は奇妙な問題を抱えています。ウェブブラウザでMP3ファイルが見つかるたびに新しいasynctaskが起動され、各AsyncTaskのリストビューに進行状況バーが表示されます。したがって、ダウンロード数は1以上の同時になる可能性があります。しかし、今Asynctaskは、プログレスバーがすべてと異なるAsyncTaskのために異なっていないため、同じ移動launcedありされるたびに、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 新しいDownloadFile()。execute(url2.toString());

答えて

3

あなたの問題はあなたが単一の静的参照を持っており、複数のプログレスバーを制御するために期待することはできません。このstatic ProgressBar pb;

です。 ProgressBarをAsyncTask内に完全にカプセル化し、インスタンス変数にします。

EDIT onProgressUpdateで

あなたは、ProgressBarの進行状況を変更する必要があります。すべての行にアダプターは必要なく、無駄です。

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

ます。また、値プログレスバーを割り当てることはありません、あなたはその変数を取り除く、pbを評価してください! PreExecuteでは、リストのアダプタを再割り当てします。

多くの変更が必要です。リストビューを管理する単一のアダプタを作成します。各行には、独自の進捗バー、イメージビュー、およびテキストビューを維持するAsyncTaskがあります。

+0

私はそれを削除しましたが、引き続き問題が続く....... :( – Programmer

4

また、ここでは()を実行上の注意事項をお読みください。

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

ハニカムた後、あなたは、おそらく同時に複数を実行することはできませんので、すべてのASyncTasksを実行している唯一のスレッドが存在します。最初の処理の後、残りの処理はキューに入れられ、最初の処理が完了するまで実行されません。おそらく、スレッドで各ダウンロードを実行し、単一のASyncTaskを使用してそれらのすべてを監視する必要があります。 (スレッドからも監視することができますが、UIスレッドのUIに更新を安全にポストするための特別な手順が必要です)。

+0

これを指摘してくれてありがとう、私は彼らがデフォルトでシリアルプールに戻ってきたことを知らなかった。 – smith324

+0

np - justそれは自分自身が難しい方法を学んだ:) – Fraser

+1

難しい方法は?ドキュメントは、それがまだ戻って変更されていないと言います。 – smith324

関連する問題