Search code examples
androidimagelistviewdynamiclistadapter

Android, how to change image-view item of list-view dynamically?


I have a list. Each row includes an Image at left and two text views on right. Based on server flag my application opens system media player or open another activity to show news detail.

In my adapter I want to add another image on top my video images (not news images). So, in XML file of Row I have another image View that its visibility is set to "Invisible" and I want to set it to "Visible" for each row which is video.

getView() method is like this:

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

            if (convertView == null) {
                convertView     = myInflater.inflate(R.layout.list_news_adapter, null);
                holder          = new ViewHolder();
                holder.ivIcon   = (ImageView)   convertView.findViewById(R.id.list_news_icon);
                holder.ivPlay   = (ImageView)   convertView.findViewById(R.id.list_news_PlayIcon);
                holder.tvTitle  = (TextView)    convertView.findViewById(R.id.list_news_title);
                holder.tvDate   = (TextView)    convertView.findViewById(R.id.list_news_date);

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

            holder.tvTitle.setText(mLatestNews.getTitle().get(position));
            holder.tvDate.setText(localTime.get(position));

            if(mLatestNews.getType().get(position).equalsIgnoreCase("VIDEO"))
                holder.ivPlay.setVisibility(View.VISIBLE);

            // Load and display image
            String imageUrl = (mLatestNews.getImageLink().get(position));
            imageUrl = imageUrl.trim().replaceAll(" ", "%20");
            imageLoader.displayImage(imageUrl, holder.ivIcon, options);

            return convertView;
    }

    static class ViewHolder {
        ImageView ivIcon;
        ImageView ivPlay;
        TextView tvTitle;
        TextView tvDate;
    }

Normally first four items of list are video and rest are news. When I run the appllication, not only 4 first items, even second image will be activated (set to visible) for some news items.

correct result is like this image: enter image description here

but after scroll of list, second image (white triangle) adds to news items. like this image: enter image description here

I have no idea why it happens. any suggestion would be appreciated.


Solution

  • Oh, my god!!!!

    It just needs "else". I changed the code like this:

    if(mLatestNews.getType().get(position).equalsIgnoreCase("VIDEO"))
                    holder.ivPlay.setVisibility(View.VISIBLE);
                else
                    holder.ivPlay.setVisibility(View.INVISIBLE);
    

    Now, it's working.