Listed progress bar in AsyncTask boot mode

I had a strange problem: I have a new synthetics that I launch whenever I find an MP3 file on a web browser, and a lauch progress bar is listed for each AsyncTask. Thus, the number of downloads can be more than 1 at the same time. But now that Asynctask is running, the ProgressBar moves the same for everyone and is no different for different AsyncTask, Plz guides me ........

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; } } } 

This is the beginning of AsyncTask and its onCreate new DownloadFile (). execute (url2.toString ());

+6
source share
2 answers

Your problem is this static ProgressBar pb;

You do not have a single static link and you can control several progress indicators. Fully encapsulate the ProgressBar inside AsyncTask, make it an instance variable.

EDIT

In onProgressUpdate you need to change the progress of the ProgressBar. You do not need an adapter for each line, its wasteful.

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

You also never assign a value to progressBar, you keep evaluating pb , get rid of this variable! In PreExecute, you reassign the List adapter.

You need a lot of changes. Create one adapter that manages the list. Each line can have AsyncTask, which supports its own progress bar, image view, and text view.

+3
source

Also, read the notes on execute () here:

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

After Honeycomb there will be only one thread executing all ASyncTasks, so you probably won't be able to run more than one at a time. After the first, the rest will stand in line and will not be executed at all until the first is over. You will probably need to complete each download in the stream and use one ASyncTask to monitor all of them. (You can also monitor the flow, but you need to take additional steps to safely add updates to the user interface in the user interface stream).

+4
source

Source: https://habr.com/ru/post/895264/


All Articles