Marl
Marl

Reputation: 1504

Android Listview: Dynamically adding images in BaseAdapter:getView cause duplicate images

I'm extending the BaseAdapter class in my listview adapter class. In this class, I dynamically add images in each row. But for some reason, I got duplicate number of images inside a row.

I tried logging while dynamically adding images in the getView and it appears that the getView was called twice. How can I fix this?

This is the snippets:

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

        HorizontalScrollView horizontalScrollView = (HorizontalScrollView)vi.findViewById(R.id.horizontallayout);
        LinearLayout imagesLayout = (LinearLayout)vi.findViewById(R.id.linearlayoutinsidehorizontal);


        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(...);
        //...
        LinearLayout.LayoutParams ilp = new LinearLayout.LayoutParams(...);
        //...

        RowData current = new RowData();
        current = data.get(position);

        if(current.hasImage()){
            //...
            ArrayList<String> images = current.getImages();
            for(int i = 0; i < images.size(); i++){
                ImageView currImage = new ImageView(mActivity);
                currImage.setLayoutParams(ilp);
                imagesLayout.addView(currImage);
                imageLoader.DisplayImage(images.get(i), currImage);
                //imageLoader is an implementation of [LazyList] see note below
                Log.d("PostListAdapter:getView", "image[" + i + "]: " + images.get(i));
            }
            horizontalScrollView.removeAllViews();
            horizontalScrollView.addView(imagesLayout);
        }

        return vi;
    }

From onCreate of the calling Activity:

list = (ListView)findViewById(R.id.listviewfromlayout);
adapter = new MyAdapter(MyActivty.this, arraylistOfData);
list.setAdapter(adapter);

Note:

I've use the LazyList to load all the images.

Upvotes: 1

Views: 1477

Answers (1)

ranjk89
ranjk89

Reputation: 1570

Here is an extract, note that you are also saving all those calls to findViewById on each view's draw call.

public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    CustomHolder dataHolder = null;
    if(convertView == null){
        vi = inflater.inflate(R.layout.myrowlayout, null);
        HorizontalScrollView horizontalScrollView = (HorizontalScrollView)vi.findViewById(R.id.horizontallayout);
        LinearLayout imagesLayout = (LinearLayout)vi.findViewById(R.id.linearlayoutinsidehorizontal);
        dataHolder = new CustomHolder(horizontalScrollView, imagesLayout);
        vi.setTag(dataHolder);
    } else {
        dataHolder = vi.getTag();
    }

    //... Do whatever with the views

    return vi;
}
// Custom Holder class
private class CustomHolder{
    HorizontalScrollView horizontalScrollView;
    LinearLayout imagesLayout;

    CustomHolder(HorizontalScrollView hsv, LinearLayout imgLayout){
        this.horizontalScrollView = hsv;
        this.imagesLayout = imgLayout;
    }
}

Upvotes: 1

Related Questions