2016-03-20 13 views
0

私はAsyncTaskを使用してリストビューダウンローダーを作成しています。ダウンロード中にリストビューに進捗バーのパーセンテージを表示したいのですが、パーセンテージが増えていません。Progress Dialogでパーセンテージを表示することがわかりましたが、リストビューで更新する方法はわかりません。ここに私のコードは次のとおりです。AndroidでListViewでダウンロード中に進捗率が増加する

FileDownloadTask.java

public class FileDownloadTask extends AsyncTask<String, Integer, String> { 
    private static final String TAG = FileDownloadTask.class.getSimpleName(); 
    final DownloadInfo mInfo; 
    public int progress; 

    public FileDownloadTask(DownloadInfo info) { 
     mInfo = info; 
    } 

    @Override 
    protected void onProgressUpdate(Integer... values) { 
     mInfo.setProgress(values[0]); 
     mInfo.setFilePercent(values[0]); 
     ProgressBar bar = mInfo.getProgressBar(); 

     if(bar != null) { 
      bar.setProgress(mInfo.getProgress()); 

     } 

    } 

    @Override 

    protected String doInBackground(String... f_url) { 
     int count; 
     try { 
      String root = Environment.getExternalStorageDirectory().toString(); 

      System.out.println("Downloading"); 
      URL url = new URL(mInfo.getFileUrl()); 

      URLConnection conection = url.openConnection(); 
      conection.connect(); 
      // getting file length 
      int lenghtOfFile = conection.getContentLength(); 

      // input stream to read file - with 8k buffer 

      // Output stream to write file 
      File rootdirectory= new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES),"Youtube Videos"); 
       if(!rootdirectory.exists()) 
       { 
        rootdirectory.mkdirs(); 
       } 
      String nameoffile= URLUtil.guessFileName(mInfo.getFileUrl(),null, MimeTypeMap.getFileExtensionFromUrl(mInfo.getFileUrl())); 
      File file= new File(rootdirectory,nameoffile); 
      file.createNewFile(); 



      mInfo.setDownloadState(DownloadState.DOWNLOADING); 
      InputStream input = new BufferedInputStream(url.openStream(), 8192); 

      OutputStream output = new FileOutputStream(file); 
      byte data[] = new byte[1024]; 

      long total = 0; 
      while ((count = input.read(data)) != -1) { 
       total += count; 
       progress= (int)((total*1001)/lenghtOfFile); 
       publishProgress(progress); 

       mInfo.setFilePercent(progress); 

       // writing data to file 
       output.write(data, 0, count); 

      } 

      // flushing output 
      output.flush(); 

      // closing streams 
      output.close(); 
      input.close(); 

     } catch (Exception e) { 
      Log.e("Error: ", e.getMessage()); 
     } 
     mInfo.setDownloadState(DownloadState.COMPLETE); 
     return null; 
    } 






    protected void onPostExecute(String file_url) { 
     mInfo.setDownloadState(DownloadState.COMPLETE); 
     // String imagePath = Environment.getExternalStorageDirectory().toString() + "/downloadedfile.mp4"; 
    } 

    @Override 
    protected void onPreExecute() { 
     mInfo.setDownloadState(DownloadState.DOWNLOADING); 
    } 


} 

DownloadInfoArrayAdapter.Java

public class DownloadInfoArrayAdapter extends ArrayAdapter<DownloadInfo> { 
    // Simple class to make it so that we don't have to call findViewById frequently 

    public static Integer result; 
    private static class ViewHolder { 
     TextView textView; 
     ProgressBar progressBar; 
     Button button; 
     DownloadInfo info; 
     TextView size; 
     TextView prog; 
    } 


    private static final String TAG = DownloadInfoArrayAdapter.class.getSimpleName(); 

    public DownloadInfoArrayAdapter(Context context, int textViewResourceId, 
            List<DownloadInfo> objects) { 
     super(context, textViewResourceId, objects); 

    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View row = convertView; 
     final DownloadInfo info = getItem(position); 
     // SignInAsyntask task1 = new SignInAsyntask(info); 
     //task1.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); 
     //new SignInAsyntask().execute(); 
     // We need to set the convertView's progressBar to null. 

     ViewHolder holder = null; 

     if(null == row) { 
      LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      row = inflater.inflate(R.layout.file_download_row, parent, false); 

      holder = new ViewHolder(); 
      holder.textView = (TextView) row.findViewById(R.id.downloadFileName); 
      holder.progressBar = (ProgressBar) row.findViewById(R.id.downloadProgressBar); 
      holder.size=(TextView) row.findViewById(R.id.downloadFileSize); 
      holder.prog=(TextView) row.findViewById(R.id.downloadFileProgress); 
      holder.button = (Button)row.findViewById(R.id.downloadButton); 
      holder.info = info; 

      row.setTag(holder); 
     } else { 
      holder = (ViewHolder) row.getTag(); 

      holder.info.setProgressBar(null); 
      holder.info = info; 
      holder.info.setProgressBar(holder.progressBar); 
      holder.info.setProgress(null); 
      holder.info = info; 
      // holder.info.setProgress(holder.prog); 
     } 

     holder.textView.setText(info.getFilename()); 
     holder.progressBar.setProgress(info.getProgress()); 
     holder.progressBar.setMax(info.getFileSize()); 
     holder.prog.setText(info.getFilePercent() + "/100"); 
     holder.size.setText(info.getFileSize() + "MB"); 

     info.setProgressBar(holder.progressBar); 
     //info.setProgress(holder.prog); 

     holder.button.setEnabled(info.getDownloadState() == DownloadState.NOT_STARTED); 
     final Button button = holder.button; 
     holder.button.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       info.setDownloadState(DownloadState.QUEUED); 
       button.setEnabled(false); 
       button.invalidate(); 
       FileDownloadTask task = new FileDownloadTask(info); 
       task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); 
      } 
     }); 


     //TODO: When reusing a view, invalidate the current progressBar. 

     return row; 
    } 
} 

助けてください!

答えて

0

代わりにコンストラクタのパラメータdoInbackground

FileDownloadTask task = new FileDownloadTask().execute(DownloadInfo info); 

@Override 
protected String doInBackground(DownloadInfo... params) { 
    info = params[0]; 
.. 

} 
0

トリックを介してデータを渡すためにしてみてください:直接ビューには何も設定しないでください。代わりに、データを変更し、ArrayAdapterでnotifyDataSetChanged()を呼び出して、ビューを変更できるようにします。アダプタは、その後で自分の上書きgetViewメソッド(int型、ビューを呼んで、あなたのアダプタのインスタンス、またはその後、アダプタに更新されたメッセージを中継するアダプタの活動へのコールバックのいずれかである必要があり例えば、

@Override 
protected void onProgressUpdate(Integer... values) { 
    mInfo.setProgress(values[0]); 
    mInfo.setFilePercent(values[0]); 

    adapter.notifyDataSetChanged(); 
} 

、Viewgroup)メソッドを使用し、更新されたデータを正しく表示します(私は後者のファンです)。

(これはまたあなたのMINFOインスタンスはもちろん、あなたのアダプタに裏打ちされた同じインスタンスであると想定される。)

編集:ちょうどポストの思考:あなたもあなたのことを確認単一インスタンスのみにすることをお勧めしますクリックすると複数の異なるダウンローダーが開始されるため、アイテムのダウンローダが一度に実行されているため、おそらくいくつかの問題が発生します。乾杯〜

+0

私はonProgressUpdateまたはdoinBackground()でadapter.notifyDataSetChanged()を書くたびに、アプリケーションがクラッシュします。 –

+0

どのようなエラーがありますか? NullPointerExceptionの場合は、アダプタへの参照がnullか、アダプタのオーバーライドされたgetView()メソッドの何かが間違っている可能性があります – Guardanis

関連する問題