thomaus
thomaus

Reputation: 6258

Change dynamically the view of every item of a ListView

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?

Upvotes: 2

Views: 1247

Answers (2)

thomaus
thomaus

Reputation: 6258

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.

Upvotes: 1

Pragnani
Pragnani

Reputation: 20155

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.

Upvotes: 1

Related Questions