ListView with progress bar on every item?

I have a listView, and on each element I added a progressBar, which should disappear after loading the image. But I could not find a way to do this. I tried to make it disappear in the getView class, but it disappears immediately after loading the image.

For example, when adding some views to the scrollView in the AsyncTask 'DoInBackground class, I could load the image, and then onPostExecute I could set the image and then delete the progressBar. It works great. I want to do something similar for listView. Can anyone help me?

I don’t know if I was understandable or not, but I can summarize that I have list_item.xml containing the image and the progress on it. And I want these progressbars to disappear after downloading and installing images.

Thanks for the help.

Here is my adapter class:

class myListAdapter extends ArrayAdapter<Items> {

    private ArrayList<Items> items;
    private Context ctx;

    public myListAdapter(Context context, int textViewResourceId,
            ArrayList<Items> items) {
        super(context, textViewResourceId, items);
        this.ctx = context;
        this.items = items;
    }

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

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

        if (v == null) {
            LayoutInflater inf = (LayoutInflater) ctx
                    .getSystemService(LAYOUT_INFLATER_SERVICE);
            v = inf.inflate(R.layout.main_list_item, null);
        }

        Items index = listContents.get(position);

        if (index != null) {

            ImageView img = (ImageView) v.findViewById(R.id.listImage);
            TextView title = (TextView) v.findViewById(R.id.listTopText);
            TextView content = (TextView) v.findViewById(R.id.listDownText);

            if (title != null)
                title.setText(index.getTitle());

            if (content != null)
                content.setText(index.getContent());

            if (img != null) 
                img.setImageBitmap(index.getImageBitmap());
            ((ProgressBar) v.findViewById(R.id.listItemProgressBar)).setVisibility(View.GONE);

        }

        return v;
    }
}

So, in this line below, I make a progress bar, but it should disappear after downloading and installing the image ... But there are no progressive bars in the added list items, what is my problem ??

 ((ProgressBar) v.findViewById(R.id.listItemProgressBar)).setVisibility(View.GONE);
+5
source share
2 answers

You can do it, but this is not the best practice. You must check Thread-Sync yourself. and Cache bitmap uses LruCache or WeakRefrences. demo demonstrates only logic.

final class MyAdapter extends BaseAdapter {

    private final HashMap<String, Bitmap> mImageMap;
    private final HashSet<String> mDownloadingSet;

    public MyAdapter() {
        mImageMap = new HashMap<String, Bitmap>();
        mDownloadingSet = new HashSet<String>();
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return NUMBER_YOU_WANT;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    public void setImage(String url, Bitmap bitmap) {
        mDownloadingSet.remove(url);
        if (bitmap != null) {
            mImageMap.put(url, bitmap);
            notifyDataSetChanged();
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = getLayoutInflater().inflate(R.layout.grid_view,
                    parent, false);
            holder = new ViewHolder();

            /***
             * find the views.
             */

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

        final String url = "";// Get the image url here.

        if (mImageMap.containsKey(url)) {
            holder.image.setImageBitmap(mImageMap.get(url));
            holder.progressBar.setVisibility(View.GONE);
        } else {
            holder.image.setImageResource(R.drawable.img_downloading);
            holder.progressBar.setVisibility(View.VISIBLE);
            if (!mDownloadingSet.contains(url)) {
                ImageDownloader task = new ImageDownloader();
                mDownloadingSet.add(url);
                task.execute(url);
            }

        }

        return convertView;
    }
}

static class ViewHolder {
    ImageView image;
    ProgressBar progressBar;
}

final class ImageDownloader extends AsyncTask<String, Void, Bitmap> {

    String url;

    @Override
    protected Bitmap doInBackground(String... params) {
            url = params[0];
        final Bitmap bitmap = fetchUrlAndDecode(params[0]);
        return bitmap;
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        mAdapter.setImage(url, result);
    }

    private Bitmap fetchUrlAndDecode(String url) {
        Bitmap bitmap = null;

        /**
         * Fetch your bitmap and decode it.
         */

        return bitmap;
    }

}
+4
source

This is your XML:

 <RelativeLayout
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:paddingLeft="5dp" >

    <ProgressBar
        android:id="@+id/progress"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:layout_below="@+id/text_progress"
        android:indeterminate="false"
        android:layout_centerInParent="true"
        android:progress="50"
        android:max="100" />

     <TextView
        android:id="@+id/text_progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textColor="#000"
        android:textStyle="bold"
        android:textSize="15dp"
        android:text="0%"/>

</RelativeLayout>

and override the getView # method:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if (vi == null)
        vi = inflater.inflate(R.layout.oportunidade_item_list, null);

    ProgressBar title = (ProgressBar) vi.findViewById(R.id.progress);
    title.setProgress(Integer.parseInt(((Oportunidade) get(position)).getProbabilidade()));
    return super.getView(position, vi, parent);
}

, , ArrayList<HashMap<?, ?>>

:

+1

All Articles