0

I am setting the background of my ImageButton in getView of adapter the problem I was facing was the background image wasn't properly set, i at first used this.

    if(exist) {
        bookmark.setBackgroundResource(R.drawable.ic_action_important);
    } 

but the button background always set the background image to important, then i used this and this solves my problem.

   if(exist) {
        bookmark.setBackgroundResource(R.drawable.ic_action_important);
    } else {
        bookmark.setBackgroundResource(R.drawable.ic_action_not_important);
    }

I don't know the reason why i have to add the else because in the xml view i set the bookmark default image to ic_action_not_important, i was wondering that why we have to use else when the image ic_action_not_important is already in the adapter view, can anyone please explain. Thanks a lot in advance.

Syed Raza Mehdi
  • 4,067
  • 1
  • 31
  • 47
  • 2
    The views get recycled, so used over and over again, that's why you always have to set the full state you want to show. http://stackoverflow.com/questions/11945563/how-listviews-recycling-mechanism-works – ElDuderino Aug 15 '14 at 08:38
  • Post ur entire getview methods – Rod_Algonquin Aug 15 '14 at 08:44

1 Answers1

2

@Elduderino Answered your question in comments. The views get recycled.

Meaning the view currently holding position #10's data, might have been holding position #3's data before you scrolled, or opened and closed the view.

In the first example, you only set the background to "important" in your if-statement when the condition is true. Meaning if the background was set to important once, it will never be set to unimportant even after it gets recycled and is used to show data at a different position.

Hence your second example is correct.

NameSpace
  • 10,009
  • 3
  • 39
  • 40
  • thank you sir it helped to clarify my concepts but still i am still confused with the part when u said that "Meaning if they view currently holding position #10's data, might have been holding position #3's data before you scrolled, or opened and closed the view." – Syed Raza Mehdi Aug 15 '14 at 09:38
  • 2
    Say the viewgGroup you are setting the background for is holding #3's data. When you scroll down (for example), #3 is no longer visible, so android makes that container eligible for re-use (recyled). It will likely be used as the viewGroup to hold a later indexes data (like #8, #9, #10, etc..). The same behavior happens anytime the viewContainer leaves the visible screen, it becomes eligible for recycling. When recycled, it still keeps the background it was previously assigned, this is by design, so that common components only need to be inflated once, unique stuff can be written over. – NameSpace Aug 15 '14 at 09:41
  • thank you sir you are great, now i get it. Thanks for clarifying my concepts, i was reading the other link here this is mentioned too "when you scroll up/down your listview is not going to create a new view it will use the view(convert view) which is already in your recycler". – Syed Raza Mehdi Aug 15 '14 at 09:43