0

I have a custom listview in which I have a Textview and Imageview now I want to hide or display the Imageview for some Items in the Listview ony.

I have done this using getview method but the problem is that when the Listview is displayed at first time the View does not get hide but when I scroll down and scroll up that time it gets hidden. following is the code snippet. Thanks in advance.

 public View getView(int position, View convertView, ViewGroup parent) {
        View v = super.getView(position, convertView, parent);
        ViewHolder holder;
        if (v != convertView && v != null) {
             holder = new ViewHolder();            
             convertView = mInflater.inflate(R.layout.jazzartist, null);
             holder.albumsView = (TextView)convertView.findViewById(R.id.artist_albums_textview);
             v.setTag(holder);


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

        ViewHolder holder1 = (ViewHolder) v.getTag();
        holder1.objimg =  (ImageView)convertView.findViewById(R.id.drag); 
        if(position == 4){  

            (holder1.objimg).setVisibility(View.INVISIBLE); // here I am hiding Imageview for position 4

            } 
        else
        {

            (holder1.objimg).setVisibility(View.VISIBLE); // here I am showing Imageview for rest of items
        }
                 String albums = getItem(position).albums;

        holder1.albumsView.setText(albums);

        return v;
      }
    }

    }
kendrelaxman
  • 439
  • 2
  • 8
  • 19

2 Answers2

1

Try below code. Hope it helps

            ViewHolder holder;
            if(convertView == null) 
            {
                 holder = new ViewHolder();            
                 convertView = mInflater.inflate(R.layout.jazzartist, null);
                 holder.albumsView = (TextView)convertView.findViewById(R.id.artist_albums_textview);
                 holder.objimg =  (ImageView)convertView.findViewById(R.id.drag); 
                 convertView.setTag(holder);
            }
            else
            {     
                holder = (ViewHolder) convertView.getTag();     
            } 

            if(position == 4)
            {
                holder.objimg.setVisibility(View.INVISIBLE); // here I am hiding Imageview for position 4
            } 
            else
            {
                holder.objimg.setVisibility(View.VISIBLE); // here I am showing Imageview for rest of items
            }

            String albums = getItem(position).albums;
            holder.albumsView.setText(albums);

            return convertView;
Braj
  • 2,164
  • 2
  • 26
  • 44
  • This as well did not work for me. In fact the view is not getting hide after I scroll down and Up. The view remain intact. any solution to this? – kendrelaxman Aug 14 '12 at 10:45
  • I have made some small changes. try once – Braj Aug 14 '12 at 10:53
  • great this has worked for.I have accepted the solution,please tell me what was the problem while scrolling? – kendrelaxman Aug 21 '12 at 04:38
  • in this solution after position 4 the Image Position seem distorted. I had placed it to extreme left but it is coming after Textviews what seems to be the problem? – kendrelaxman Aug 21 '12 at 05:06
  • Actually, in your original code you were using 2 viewHolders. I made it to 1. Using viewHolder pattern, its quite easy to control views in getView method. As far as new problem is concerned, I think something wrong with inflated xml file. If u can post that inflated xml file also, it would be nice. – Braj Aug 21 '12 at 05:51
  • yes you are right. the problem was with the xml, I am trying to set Imageview to rightaligned in a linear layout. – kendrelaxman Aug 21 '12 at 11:45
0

Please remove the conditions i.e if else (don't put check for convertView)

public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;
           holder = new ViewHolder();            
         convertView = mInflater.inflate(R.layout.jazzartist, null);
         holder.albumsView = (TextView)convertView.findViewById(R.id.artist_albums_textview);


    holder1.objimg =  (ImageView)convertView.findViewById(R.id.drag); 
    if(position == 4){  

        (holder1.objimg).setVisibility(View.INVISIBLE); // here I am hiding Imageview for position 4

        } 
    else
    {

        (holder1.objimg).setVisibility(View.VISIBLE); // here I am showing Imageview for rest of items
    }
             String albums = getItem(position).albums;

    holder1.albumsView.setText(albums);

    return convertView;
  }
Atul Bhardwaj
  • 6,647
  • 5
  • 45
  • 63