>
> May I ask why we shouldn't modify the view after it has been displayed?
>
Certainly... It is pretty much a basic principle for good OO design. Your
adapter represents the data that needs to be displayed. Your listview item
views should do nothing more than use the data it has to decide how it
should be displayed.
Let's take a look at how listviews and adapters work in Android... Let's
say your adapter has 100 data items in it. In your case each data item
consists of a string (item name) and a price (int or float). Even though
your adapter has 100 data items in it, the listview does not contain 100
views... It holds on to a few more views than can be displayed on the
screen at any given time. So, if your screen can display 10 list view
items at a time, the listview itself will only have about 13 or 14 actual
views that it uses. When you scroll, it reuses those views to save on
memory and the time it takes to create a brand new view.
So, what happens if the user clicks on a view and you change the price in
the view but not the adapter (data item)? When the user scrolls away and
comes back the value that was changed will be lost and the original value
will still be displayed. To get around that you could change both the view
AND the adapter, but then you are violating the DRY principle: Don't Repeat
Yourself. If you are making the same changes in different places then you
are probably doing something wrong and there is a better way to go about
doing it.
Have you ever noticed how hard it is to get a reference to an individual
list view item? There is a reason for that... it is only intended to be a
display of your underlying data model. Anything that you want to do to
change the views should be done in the data model. That way, you change
things in one place, call notifyDataSetChanged() and then everything
magically updates itself.
Now, this means that if you are going to have a complex set of data
(meaning typically more than a single type of data in the view) then you
will most likely want to subclass one of the existing Adapter classes. I
usually subclass BaseAdapter to suit my needs. You also will need to
implement your getView() method in the adapter properly. That is where you
use the data model to determine how the list view item should be
displayed. I would recommend looking up the Android View Holder pattern
and implementing that in this method as well, since that will help speed up
the scrolling of your listviews.
> Does the "no modification" rule apply to moving the views around?
>
I guess that would depend on how it is done. This case is obviously a
little different from what the OP was wanting to do.
I've got a ListView object, where I'm using the "TouchListView.java"
> adaption to allow the user to resort the items
>
I've not familiar with that so I can't really comment on it.
Does this movement qualify as a "modification"? I'm performing data
> changes within the Adapter, but the actual view items are getting
> moved through the touch interface / view code.
>
As long as you are updating your adapter and not duplicating code by also
manually updating the views you are probably ok. If you are duplicating
some code you may want to look and see if there are some ways you can
optimize what you are doing.
Hope this helps clarify things a bit!
Thanks,
Justin Anderson
MagouyaWare Developer
http://sites.google.com/site/magouyaware
On Mon, Mar 26, 2012 at 1:21 AM, Danny D wrote:
> Does the "no modification" rule apply to moving the views around?
>
> I've got a ListView object, where I'm using the "TouchListView.java"
> adaption to allow the user to resort the items
>
> Does this movement qualify as a "modification"? I'm performing data
> changes within the Adapter, but the actual view items are getting
> moved through the touch interface / view code.
>
> DD
>
> On Mar 25, 11:29 pm, Jason Tian wrote:
> > Hi, Justin
> >
> > May I ask why we shouldn't modify the view after it has been
> > displayed?
> > Cause I've been doing this a lot in my app...
> >
> > Thanks,
> > Jason
> >
> > On Mar 26, 9:12 am, Justin Anderson wrote:
> >
> >
> >
> >
> >
> >
> >
> > > Modify the data in the adapter and then call notifyDataSetChanged().
> Once
> > > the view has been created and displayed on the screen you should NEVER
> > > modify it directly.
> >
> > > Thanks,
> > > Justin Anderson
> > > MagouyaWare Developerhttp://sites.google.com/site/magouyaware
> >
> > > On Sat, Mar 24, 2012 at 5:44 AM, Akram wrote:
> > > > This listview has an adapter that is linked to a layout for each row
> > > > (itemrow.xml)
> >
> > > > The price value (500-250 300) is a textview
> >
> > > > How can we access to it, to modify it, once clicked on a button ?
> >
> > > > Thanks you for the help.
> >
> > > > l
> >
> > > > Android
> >
> > > > Price: 500
> >
> > > > l
> >
> > > > Php
> >
> > > > Price: 250
> >
> > > > l