Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ListView content background changed when I scroll down and up.

 
Snehitha Prasad
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am new to Android.I am developing one small application,with in that I am using a List View and getting the list content from web service call.
for this I am using Base Adapter,with in this I am checking some condition.If the condition is true I am changing the background of the content.If not keeping as it is.

The problem is when I go to this page which is displaying good ,what I have expected.But when I scroll down and up then that background color is applied for the List View content even though which didn't satisfy the condition.

Here is my Adapter Class.


Can anyone give some Idea on this.

Thanks in Advance,
SNEHITHAPRASAD
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The views in the adapter are re-used over and over again. So when the contents of one of the views requires the change in background, it is applied to that view. Then later another content is applied to the same view, but this new content does not require the different background. Since you do not undo the background change, the background is left the way it is - with the conditional background turned on even though you do not want it to be.

You should check to see if the view is being re-used, and if it is, revert it to a default configuration. For example, you have the if (convertView == null) { ... } else { ... } structure. That else statement is a good place to assign the default background.
 
Snehitha Prasad
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for your response.When I go the layout first time which is going to the if condition and created the layout what I get the web-service call and which satisfies my requirement.
But when I scrolled now the control goes to else condition and the view is going to be reused.But I am still wondering,why the background is changed unexpectedly irrespective of the condition.

Can you please suggest on this.

Thanks,
SNEHITHAPRASAD.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Snehitha Prasad wrote:...the view is going to be reused.But I am still wondering,why the background is changed unexpectedly irrespective of the condition.

It is not changed irrespective of condition. It is changed when the proper conditions are met, and the same view which had the background changed is reused. The background remains the same because it is never reverted back since you never write code which reverts it back.

If you had a red box and you put some fruit in it and then painted it blue. When you take the fruit out of the box and put clothing in it instead would you expect the box to be red again? No, it would still be blue because that is the last color you painted it. If you wanted the box to be red again you need to paint it red. The same thing is happening in your code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic