Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8489803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:55:47+00:00 2026-06-10T21:55:47+00:00

I am working on a Download Manager project, so, to show all downloaded /

  • 0

I am working on a Download Manager project, so, to show all downloaded / downloading actions, i prefer to use ListView to show my download list. Suppose that, we have as many as downloading task, so, the progress bars of all tasks must be updated. For background download task, i created a new class that i named it HttpDownloader. So, i pass these progress bars on objects of this class. When a new object is added to my tasklist, so, i call the constructor of HttpDownloader and pass the new item progress bar to it. The thing confused me is When i add a new object to tasklist and call notifyDataSetChanged of adapter, my list is refreshed, so all progress bar reset to default layout values but HTTPDownloader Thread is running in background successfully. So, it is my question that,

1. After calling notifyDataSetChanged, references to old listview’s objects are destructs ?

2. If yes, How can i keep old view’s reference ?

3. If no, please explain me, why progress bars reset to default and do not change when background process force to passed progressbar to change the value of progress ?

HTTPDownloader class

class HttpDownloader implements Runnable {
    public class HttpDownloader (String url, ProgressBar progressBar)
    {
        this.M_url = url;
        this.M_progressBar = progressBar;
    }

    @Override
    public void run()
    {
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet(this.M_url);
        HttpResponse response;

        try{
            response = client.execute(get);
            InputStream is = response.getEntity().getContent();
            long contentLength = response().getEntity().getContentLength();
            long downloadedLen = 0;
            int readBytes = 0;

            byte [] buffer = new byte [1024];


            while ((readBytes = in.read(buffer, 0, buffer.length)) != -1) {
                downloadedLen += readBytes;

                //Some storing to file codes

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        M_progressBar.setProgress((100f * downloadedLen) / contentLength);
                    }
                });
            }

            is.close();
        } catch (ClientProtocolException e) {
            Log.e("HttpDownloader", "Error while getting response");
        } catch (IOException e) {
            Log.e("HttpDownloader", "Error while reading stream");
        }
    }
}

AdapterClass

class MyAdapter extends ArrayAdapter<String> {
    ArrayList<String> M_list;
    public MyAdapter(ArrayList<String> list) {
        super(MainActivity.this, R.layout.download_item, list);
        this.M_list = list;
    }

    @Override
    public int getCount() {
        return this.M_list.size();
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.download_item, parent, false);
        ProgressBar bar = (ProgressBar) rowView.findViewById(R.id.progrees);

        new Thread (new HttpDownloader(this.M_list.get(position), bar)).start();

        return rowView;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T21:55:49+00:00Added an answer on June 10, 2026 at 9:55 pm

    Well if you want to keep the references to show a good download manager, lets see how do it, go by parts.

    The concept is easy to understand, we have threads downloading items, we are going to call each of this piece of work tasks. Each task is associated with a row in the list view, because you want to show a download progress of each item download.

    So if each task is associated with each row, we are going to save tasks actually are in execution, in this way, we know in all moment what tasks are running and their state. This is importante because, like I have said before, getView() method is called many times, and we need to know each download in progress and its state.

    Lets see some code of our Adapter:

    class MyAdapter extends ArrayAdapter<Uri> {
        ArrayList<Uri> M_list;
        Map<Uri, HttpDownloader> taskMap;
    
        public MyAdapter(ArrayList<Uri> list) {
            super(MainActivity.this, R.layout.download_item, list);
            this.M_list = list;
    
            taskMap = new HashMap<Uri, HttpDownloader>();
        }
    
        @Override
        public int getCount() {
            return this.M_list.size();
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View rowView = inflater.inflate(R.layout.download_item, parent, false);
    
            ProgressBar progressBar = (ProgressBar) rowView.findViewById(R.id.progressBar);
    
            configureRowWithTask(position, progressBar);
    
            return rowView;
        }
    
        private void configureRowWithTask(int position,final ProgressBar bar) {
            Uri url = M_list.get(position);
            HttpDownloader task;
    
            if (!(taskMapHasTaskWithUrl(url))) {
                task = new HttpDownloader(url, bar);
                Thread thread = new Thread(task);
                thread.start();
                taskMap.put(url, task);
            } else {
                task = taskMap.get(url);
                bar.setProgress(task.getProgress());
                task.setProgressBar(bar);
            }
        }
    
        private boolean taskMapHasTaskWithUrl(Uri url) {
            if (url != null) {
                Iterator taskMapIterator = taskMap.entrySet().iterator();
    
                while(taskMapIterator.hasNext()) {
                    Map.Entry<Uri, HttpDownloader> entry = (Map.Entry<Uri, HttpDownloader>) taskMapIterator.next();
                    if (entry.getKey().toString().equalsIgnoreCase(url.toString())) {
                        return true;
                    }
                }
            }
    
            return false;
        }
    }
    

    Some explanations about it:

    • I have changed your type of adapter to ArrayAdapter of Uris to work better with links.
    • I have added a Map wich has pairs key-value, keys are Uris and values are HttpDownloaders. This map will save each task in use.
    • I have added two important methods, one is configureRowWithTask(), like its own name says configures a row with a task, associating them, but only if the task is new, in other case is a task in use.
      The other method is taskMapHasTaskWithUrl(), simply checks if the given task (by its url) is currently in the taskMap.
    • Probably, the most important method is configureRowWithTask(), I am going to explain it deeply.

    configureRowWithTask() method checks if one task is in use, this method is needed because getView() is called many times, but we dont want the same downloading task be executed more than once, so this method checks that, if the task is new (not present in the taskMap) then creates a new instance of HttpDownloader and put the new task in the map.

    If the task requested is present in the taskMap, it means that the task has been requested previously but the row in the list has gone out and there have been a new call to getView() method. So the task is present, we dont have to create it again, probably the task is downloading the item and the only thing we have to do is see its download progress and set it to the row’s progress bar. And finally sets the reference to the progress bar, probably the HttpDownloader has lost this reference.

    With this part clear, we go with the second part, the HttpDownloader class, lets see some code:

    public class HttpDownloader implements Runnable {
    
    private Uri url;
    
    private ProgressBar progressBar;
    private int progress = 0;
    
    private Handler handler;
    
    public HttpDownloader (Uri url, ProgressBar progressBar) {
        this.url = url;
        this.progressBar = progressBar;
        progressBar.setProgress(progress);
    
        handler = new Handler();
    }
    
    @Override
    public void run() {
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet(url.toString());
        HttpResponse response;
    
    
        try {
            response = client.execute(get);
            InputStream is = response.getEntity().getContent();
            long contentLength = response.getEntity().getContentLength();
            int downloadedLen = 0;
            int readBytes = 0;
    
            byte [] buffer = new byte [1024];
    
    
            while ((readBytes = is.read(buffer, 0, buffer.length)) != -1) {
                downloadedLen += readBytes;
    
                //Some storing to file codes
    
                progress = (int) ((100f * downloadedLen) / contentLength);
    
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (progressBar != null) {
                            progressBar.setProgress(progress);
                        }
                    }
                });
            }
    
        } catch (ClientProtocolException e) {
            Log.e("HttpDownloader", "Error while getting response");
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("HttpDownloader", "Error while reading stream");
        }
    }
    
    public int getProgress() {
        return progress;
    }
    
    public void setProgressBar(ProgressBar progressBar) {
        this.progressBar = progressBar;
    }
    }
    

    This class is basically the same, the diference is that I use a Handler to change the progress of the progress bar in the UI thread, and change this progress only if the reference to the progress bar is not null.

    Important thing is every time bytes are downloaded, value of download progress is refreshed.

    I have checked the code in a test app and seems that all runs ok, if you have some problems, let me know and I will try to help you ; )

    Hope you understand it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently working on project, Android Internet Download Manager. In order to download the
I'm working on a WPF download manager which should support downloading multiple files at
I'm working on a simple download manager wrote in Cocoa and actually I use
I am working on a download manager, that will support http/https/ftp protocols. I am
I'm currently working on creating a Download Manager for Android. In order to optimize
I'm working on a console application that tries to download some files from a
I m working on a console application that tries to download some files from
I am working on a project where we have to download videos from DVR
I am working on blackberry project where i want to download image & save
i am working to make a Android application that can upload and download data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.