Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Listview shows wrong images while scrolling

Tags:

android

i am downloading images from internet using universal image loader. Some pictures are being set as avatars so i'm rounding them and others are being stretched. the problem is the listview shows wrong image when scrolling and after a while it shows the correct image. i've searched stackoverflow for similar problem but i cant get it to work rightly.

i'm using custom adapter and the getview code and the code for rounding and streching image are as follows:

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

    final ViewHolder holder;
    if(view == null)
    {
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.whatsnew_listitem, null);

        holder = new ViewHolder();
        holder.playButton = (ImageButton) view.findViewById(R.id.btn_playVideo);
        holder.mediaThumbnail = (ImageView) view.findViewById(R.id.imageview_mediaImage);
        holder.avatar = (ImageView) view.findViewById(R.id.imageview_avatar);
        holder.artistName = (TextView) view.findViewById(R.id.textview_artistName);
        holder.timestamp = (TextView) view.findViewById(R.id.textview_timestamp);
        holder.status = (TextView) view.findViewById(R.id.textview_title);
        view.setTag(holder);

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

    pos = position;
    Item item = items.get(position);

    if(item.getArtistName() != "")
    {
        // set thumbnail
        String thumbnailURL = "";
        holder.mediaThumbnail.setVisibility(View.INVISIBLE);
        if(item.getUpdateType() == Config.UPDATE_TYPE_1)
        { 
            holder.playButton.setVisibility(View.GONE);
            thumbnailURL = item.getPictureUri();

            if(thumbnailURL != null) 
            {

                ImageLoader.getInstance().loadImage(thumbnailURL, new SimpleImageLoadingListener() {
                    @Override
                    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {

                        setImage(loadedImage, imageView, Config.MEDIA);
                        mediaImages[position] = true;
                        imageView.setVisibility(View.VISIBLE);

                    }
                });
            }

            holder.status.setText(item.getMessage());
        } 
        else if(item.getUpdateType() == Config.UPDATE_TYPE_2) 
        {
            //play button
            holder.playButton.setVisibility(View.VISIBLE);

            //set media image
            thumbnailURL = item.getVideoThumbnailUri();
            if(thumbnailURL != null)
            {
                ImageLoader.getInstance().loadImage(thumbnailURL, new SimpleImageLoadingListener() {
                    @Override
                    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) 
                    {
                        setImage(loadedImage, imageView, Config.MEDIA);
                        mediaImages[position] = true;
                        imageView.setVisibility(View.VISIBLE);
                    }
                });
            }

            //set youtube status
            holder.status.setText(item.getTitle());
            holder.playButton.bringToFront();
            holder.playButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                    if(item.getVideoId()!= null) {
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setData(Uri.parse(Config.YOUTUBE_URI + item.getVideoId() ));
                        context.startActivity(intent);
                    }
                }
            });
        }

        //set avatar

        String avatarURL = item.getAvatarImageUri();
        holder.avatar.setVisibility(View.INVISIBLE);
        if(avatarURL != null)// && avatarImages[position] == false)
        {
            ImageLoader.getInstance().loadImage(avatarURL, new SimpleImageLoadingListener() {
                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {

                    setImage(loadedImage, imageView, Config.AVATAR);
                    avatarImages[position] = true;
                    imageView.setVisibility(View.VISIBLE);

                }
            });
        }

        //set artist name
        holder.artistName.setText(item.getArtistName());

        //set timestamp
        if(timerList[position] == false)
        {
            updateTime(holder.timestamp, item);
        }
    }


    view.setTag(holder);
    return view;
}

//@Override
public void setImage(final Bitmap bitmap, final ImageView imageView, final String imagetype) {
    // TODO Auto-generated method stub

    Activity activity = (Activity) context;
    activity.runOnUiThread(new Runnable() {

        @Override
        public void run() {

            Bitmap croppedBmp = null;
            int imageHeight = bitmap.getHeight();
            int imageWidth = bitmap.getWidth();

            if(imagetype == Config.MEDIA) 
            {            
                if(imageHeight <imageWidth) {
                    croppedBmp = Bitmap.createScaledBitmap( bitmap, dp, dp, true);
                    imageView.setImageBitmap(croppedBmp);
                }
            } 
            else if(imagetype == Config.AVATAR) 
            {

                int radius;
                if(imageWidth >  imageHeight) 
                {
                    croppedBmp = Bitmap.createBitmap(imageHeight, imageHeight, Bitmap.Config.ARGB_4444);
                    radius = imageHeight/2;
                } 
                else 
                {
                    croppedBmp = Bitmap.createBitmap(imageWidth, imageWidth, Bitmap.Config.ARGB_4444);
                    radius = imageWidth/2;
                } 

                Canvas canvas = new Canvas(croppedBmp);      
                final int color = 0xff424242;
                final Paint paint = new Paint();
                final Rect rect = new Rect(0, 0, imageWidth, imageHeight);

                paint.setAntiAlias(true);
                canvas.drawARGB(0, 0, 0, 0);
                paint.setColor(color);
                canvas.drawCircle(radius, radius, radius, paint);
                paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
                canvas.drawBitmap(bitmap, rect, rect, paint);

                imageView.setImageBitmap(croppedBmp);
                ImageLoader.getInstance().displayImage(uri, imageView, listener)
                notifyDataSetChanged();

            }
        }
    });

}
like image 641
Sumia Avatar asked Oct 17 '13 12:10

Sumia


1 Answers

try this example http://android.amberfog.com/?p=296

use this method

public int getItemViewType(int position) {
        return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}

and get the type as follows in getView()

int type = getItemViewType(position);

I solved my issue following the example

like image 142
cham Avatar answered Nov 02 '22 23:11

cham