Bhavesh Rangani
Bhavesh Rangani

Reputation: 1510

Listview with downloading Horizontal progress bar strange behaviour

I have custom adapter in that I have design custom view for the list view. In the row of custom view I have specify one button and Horizontal progress bar, on click of button start the downloading with progress. problem occurs when I scroll the list view. Suppose I start the downloading item at position 1st (current progress 50%) and I scroll to the last. After I scroll up and reach to the 1st at that time my 1st record start from (progress 0%).

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder = null;

    if(convertView == null) {

        holder = new ViewHolder();

        convertView = LayoutInflater.from(context).inflate(R.layout.list_items_aaj_ni_varta,parent, false);

        holder.txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
        holder.pBarDownloading = (ProgressBar) convertView.findViewById(R.id.pBarDownloading);
        holder.btnDownloadOrCancel = (ImageButton) convertView.findViewById(R.id.btnDownloadOrCancel);

        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.txtTitle.setText(listVarta.get(position).getVarta_title());

    holder.btnDownloadOrCancel.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            /*MyDownload is my custom design download class,
             * in that i have specified downloading task
            */
            MyDownload d = new MyDownLoad();
            d.setContext(context);
            d.setURl(url);
            d.setProgressBar(holder.pBarDownloading);

            d.startDownload();
        }
    });

    return convertView;
} 

Upvotes: 0

Views: 152

Answers (1)

Georgiy Shur
Georgiy Shur

Reputation: 840

The important feature of ListView, that it recycles its list items that aren't visible for preserve memory. So, the states of your views will be reset as the list item will be recycled.

One possible solution is to save your progress for each item in some list or other object inside adapter. From the background download task, you can update this data. And then, in getView() method, you could restore your progress from this data object/list.

Upvotes: 3

Related Questions