2

I want to change the visibility of an image of each item of a ListView when clicking on an external button (I mean which is not part of the ListView).

So I need to access all the children views of a ListView. Then I did a loop like this one :

for (int i = 0...
{
     View child_view = list_view.getChildAt(i);
     ...
}

The problem is that, this way, I can only access the currently visible children, and I need to access the view of every child of the ListView.

How can I do such a thing?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
thomaus
  • 6,170
  • 8
  • 45
  • 63
  • From where you are loading images is it from the internet or from the drawable resources..? – Pragnani Feb 21 '13 at 19:11
  • Have you tried that...... – Pragnani Feb 21 '13 at 20:01
  • If you have time, consider watching this video: [Google I/O 2010 - The world of ListView](http://youtu.be/wDBM6wVEO70). It's about an hour, and is worth the time. –  Feb 21 '13 at 20:28

2 Answers2

1

Solution is simple

in Your Adapter class take a int array like this

int visibilities[]={ImageView.VISIBLE,ImageView.GONE,ImageView.INVISIBLE};

if you are setting the images from the Drawable Resources than You probably pass the ids to the ArrayList items something like this

CountriesList.add(new Country("India",R.drawable.india));

Than create another variable in the Country class(Your Bean class(Getter setter class)) and set it type int something like this

class Country{
String name;
int flag;
int visibility;
}

pass the visibility like this while adding elements to your list

 CountriesList.add(new Country("India",R.drawable.india,1));

And in your getView() method of the Adapter you will probably set the resource like this

holder.image.setImageResource(country.getFlag());

also add this line

holder.image.setVisibility(visibilities[country.getVisibility]);

In order to explain this I have added country example.. I Have used the same trick in my previous app for the list... and it worked like charm.. I hope this will help you.

Pragnani
  • 20,075
  • 6
  • 49
  • 74
1

I solved the problem by:

  • changing all the visible children with this : enter link description here

  • changing the non-visible children in the function getView of the adapter because, and it's important, getView refreshes on scrolling.

Community
  • 1
  • 1
thomaus
  • 6,170
  • 8
  • 45
  • 63