Adding to hoyski's reply:
Purpose of convertView argument:
Its all discussed in some Google I/O video by Romain. (Ref: Kumar Bibek's
reply).

Its well known that when you update the data that backs-up a listview, you
are supposed to call notifydatasetchanged() when you want the data
changes(for eg: changing the element thats supposed to be colored) to be
made visible to user. This internally triggers getView() for each of the
elements *visible* on screen (assuming visibility changes by scrolling).
Note that there could be multiple calls to getView. Avoid using wrap_content
for height of ListView, its recommended to reduce calls to getView.

Generally you would create a list elements's view by code or by inflating an
XML layout. These are expensive operations, especially
inflating. List/GridView maintain a recycler module that helps in reusing
Views that were created during initial calls to getView().

Whenever a view goes out of visibility, it goes into recycler and the widget
tries to re-use it by passing the View object as convertView argument to
getView calls for data elements that are now visible. We know that every
View is a tree of views. Inside getView, if you can confirm that convertView
is non-null (meaning there were spare re-usable views in the recycler) and
has the same  tree structure as the current data object expects, then you
could just update the attributes of its children(for eg: bg color of child
textview, colored if data element selected, default if not selected) and
return convertView from getView(). If you are sure that all ListView
elements are gonna have same layout then you can just do away with the null
check as you are sure you will find the textview as its child.

I am not sure if implementing the color change logic inside onItemSelected
would be full proof as the View objects associated with each data object is
dynamic, changes with each call to getView(). For the above approach you
could use  onItemSelected to update the data object that's selected and
reset the data object that was selected previously and call
notifyDataSetchanged.

On Thu, Dec 2, 2010 at 8:07 AM, hoyski <[email protected]> wrote:

> ArrayAdapter is a bridge between all of the items that can potentially
> be displayed (the items in your array) and those currently on the
> screen. Only the ones on the screen need to have View objects.
> getView() is an optimization method that allows you to reuse
> previously constructed Views as items scroll off the screen and others
> scroll on. If the View that's received in getView() is non-null and of
> the right type for the array item at the given position that's coming
> onto the screen, then all you need to do is set the state of all of
> the items in the View by calling setText(), setChecked(), etcetera to
> match the state of the array item.
>
> In your case it sounds like an action performed on one item in your
> array, via its View, can affect the state of another item in your
> array. If that's the case, then all you need to do is update the state
> of the affected item(s) in your array when the action is performed,
> and then call your array adapter's notifyDataSetChanged() method. This
> will force a refresh of all of the items currently displayed.
>
> On Dec 1, 2:53 pm, kiros88 <[email protected]> wrote:
> > Well okay so my main purpose to asking was in my app i wanted the user
> > to be able to select the textview from the listview and once the user
> > touches it the text changes colors. but i want also that if the user
> > touches a new text the original one he touch goes back to the original
> > color while the new one he touch changes colors. so to do taht i need
> > to be able to find the previous textview taht i manipulated during the
> > setOnClickListenerView so i figure if i can find the textview from the
> > array i should be able to get the previous one but im still just
> > confused with the call because i dont get the argument in the middle
> > taht u send a "View convertView"
> >
> > On Dec 1, 10:34 am, Prakash Iyer <[email protected]> wrote:
> >
> > > Isn't getView called from the system and you override it to give back a
> view
> > > typically to display a list item? You say that you are calling getView
> - any
> > > specific reason?
> >
> > > On Wed, Dec 1, 2010 at 1:22 PM, kiros88 <[email protected]> wrote:
> > > > so basically im trying to figure out what "View convertView" means
> > > > exactly. On the definition it says
> >
> > > > "The old view to reuse, if possible. Note: You should check that this
> > > > view is non-null and of an appropriate type before using. If it is
> not
> > > > possible to convert this view to display the correct data, this
> method
> > > > can create a new view."
> >
> > > > so if I call getView should my convertView be a default view in case
> i
> > > > can't get get the view from my adapterView?
> >
> > > > --
> > > > You received this message because you are subscribed to the Google
> > > > Groups "Android Developers" group.
> > > > To post to this group, send email to
> [email protected]
> > > > To unsubscribe from this group, send email to
> > > > [email protected]<android-developers%[email protected]><android-developers%2Bunsubs
> [email protected]>
> > > > For more options, visit this group at
> > > >http://groups.google.com/group/android-developers?hl=en
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]<android-developers%[email protected]>
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to