为什么BaseAdapter的getView()多次显示图像?

时间:2012-10-30 09:17:59

标签: android list listview baseadapter

我正在使用以下代码制作自定义listview。我必须动态显示图像,因此所有图像都会添加到Linearlayout中。问题是这些动态图像会多次添加。以下是我getView()的代码。

    LinearLayout fbpiclayout = null;

        if (convertView == null)
            vi = inflater.inflate(R.layout.popular_event_list, null);

         fbpiclayout = (LinearLayout) vi
                .findViewById(R.id.fbpic_layout);

        ArrayList<String> list=  mDbManager
                .getAllValuesComingSoonFriendList(facebookEventList
                        .get(position).getEventId());


        int height = 0;


        for(int i=0;i<list.size();i++)
        {
            if(i<3)
            {
                String friendPics = "http://graph.facebook.com/"
                        + list.get(i)
                        + "/picture?type=large";

                Log.d("Friends","list no "+i+" "+mDbManager
                        .getFacebookFriendName(list.get(i)));

                ImageView fbFriendimage = new ImageView(getActivity());
                LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams(
                        LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
                vp.setMargins(3, 0, 3, 3);
                fbFriendimage.setLayoutParams(vp);
                fbFriendimage.setAdjustViewBounds(true);
                fbFriendimage.getLayoutParams().height = height;

                fbFriendimage.getLayoutParams().width = width_iv;
                fbFriendimage.setScaleType(ImageView.ScaleType.CENTER_CROP);
            //  image.setImageBitmap(bm);
                imageLoader.DisplayImage(friendPics, fbFriendimage);
                fbpiclayout.addView(fbFriendimage, vp);     

            }
        }

请在这个问题上建议我。 在此先感谢。

2 个答案:

答案 0 :(得分:0)

请尝试以下代码:

LinearLayout fbpiclayout = null;

        if (convertView == null)
            vi = inflater.inflate(R.layout.popular_event_list, null);

         fbpiclayout = (LinearLayout) vi
                .findViewById(R.id.fbpic_layout);

          //if the convertView was not null it would have the child view you added the 
          //  last time liner layout was used. So remove the added child view.  
          fbpiclayout.removeAllViews();

        ArrayList<String> list=  mDbManager
                .getAllValuesComingSoonFriendList(facebookEventList
                        .get(position).getEventId());


        int height = 0;


        for(int i=0;i<list.size();i++)
        {
            if(i<3)
            {
                String friendPics = "http://graph.facebook.com/"
                        + list.get(i)
                        + "/picture?type=large";

                Log.d("Friends","list no "+i+" "+mDbManager
                        .getFacebookFriendName(list.get(i)));

                ImageView fbFriendimage = new ImageView(getActivity());
                LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams(
                        LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
                vp.setMargins(3, 0, 3, 3);
                fbFriendimage.setLayoutParams(vp);
                fbFriendimage.setAdjustViewBounds(true);
                fbFriendimage.getLayoutParams().height = height;

                fbFriendimage.getLayoutParams().width = width_iv;
                fbFriendimage.setScaleType(ImageView.ScaleType.CENTER_CROP);
            //  image.setImageBitmap(bm);
                imageLoader.DisplayImage(friendPics, fbFriendimage);
                fbpiclayout.addView(fbFriendimage, vp);     

            }
        }

EDIT1:尝试使用智能图片视图http://loopj.com/android-smart-image-view/来提高效果....

答案 1 :(得分:0)

不要使用该循环。将该列表的大小放在getCount()中。它会正常工作