0

i'm using an ArrayAdapter in my ListView.

I can't really understand if when i'm setting and ArrayList to my ArrayAdapter, it recreate all the objects of it, or just reference to it.

for some reason same objects which are supposed to be the same in too list views, doesn't get effected with property changes as if the current object is a clone of the other object..

can anyone please clear that out ?

Asaf Nevo
  • 11,338
  • 23
  • 79
  • 154

3 Answers3

1

When you pass ArrayList to an ArrayAdapter, it actually manipulates object using Pass-by Reference.

And when you alter the contents of your ArrayList and want to reflect those changes in ArrayAdapter, then you must need to call notifyDataSetChanged() method of your adapter.

P.S: Practically you should pass a cloned object of your ArrayList to adapter (unless you know what you are doing), since its unnecessary and will take extra resources.

More info.

Community
  • 1
  • 1
waqaslam
  • 67,549
  • 16
  • 165
  • 178
  • isn't there a way which by i can change an object in the ArrayList, update the changes in the ArrayAdapter, and the object in the other adapter will change to ? it is referenced as the same object... – Asaf Nevo Sep 04 '12 at 15:24
  • when you call *notifyDataSetChanged()* it actually asks adapter to re-read the source contents hence reflecting the changes. However, if you add a new item in adapter using `adapter.add` then you dont need to call notifyDataSetChanged because then the adapter refreshes itself automatically after the insertion. I'm not sure if this is what you meant to in your comment :) – waqaslam Sep 04 '12 at 15:29
  • it's not :) let me explain. i have to ListViews. in both i have the same object. i'm changing one of the properties in one of them, and the change is reflected only in the current listview and not in the other.. why ? – Asaf Nevo Sep 04 '12 at 15:36
  • because your second adapter is not aware of the underlying change which has already occurred on your referenced ArrayList. Unless you use the same ArrayAdapter object for both of your ListViews or call notifyDataSetChanged on your second ArrayAdapter too to reflect the changes. – waqaslam Sep 04 '12 at 16:21
  • in my code i have a lazy download of photo from a url. after the lazy download finishes it shows the image in the row of the list view, and set it as a drawable property in the object of the raw.. but i see that when i'm refreshing my list view, it does the whole process again as if it didn't already downloaded it and sign it to property.. why ? – Asaf Nevo Sep 05 '12 at 07:41
  • I'm not sure how you are dealing with lazy download but what you can do is to check the availability of image in the cache folder on sd-card. If its not available then download and save it in the cache, so that next time when you scroll through records, you refer to the previously downloaded image and does not need to download again – waqaslam Sep 05 '12 at 08:11
0

It depends on how you create the ArrayAdapter.

If you use a version of the constructor that does not take an array or List as a parameter, then it will create a new list that each adapter instance maintains. If you create your adapter with one of the following versions:

ArrayAdapter(Context context, int textViewResourceId, List<T> objects)

ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects)

Then you can supply the same external list to multiple adapters and change your data set only once, because in this case the adapter just references your list and doesn't copy the data.

devunwired
  • 62,780
  • 12
  • 127
  • 139
0

Both cases are possible, here explain how the second case helps optimize your listviews: http://developer.android.com/training/improving-layouts/smooth-scrolling.html

JMPergar
  • 1,906
  • 1
  • 19
  • 21