1

Is there a difference between:

@Override void onPostExecute(ArrayList<Items> rows) {  
   this.dataset.clear();  
   this.dataset.addAll(rows);  
   this.dataAdapter.notifyDataSetChanged();  
}  

and

@Override void onPostExecute(ArrayList<Items> rows) {  
   this.dataset.clear();  
   this.dataset = rows;  
   this.dataAdapter.notifyDataSetChanged();  
}  

Both seem to work correctly but most of the examples I have seen about this use the first pattern.
Is the second wrong?

Jim
  • 18,826
  • 34
  • 135
  • 254
  • On a cursory look, it seems first one assumption is you have already declared the variable as this.dataset = new ArrayList so that it doesn't throw the null pointer somewhere prior to this call and that is standard practice. Second I think doesn't require the new call. – sumandas Aug 17 '16 at 16:00
  • semantically the two operations are different. – Blackbelt Aug 17 '16 at 16:01
  • 1
    @sumandas if `this.dataset` is `null` in the second example, `this.dataset.clear()` would throw a NPE. – Ed Holloway-George Aug 17 '16 at 16:05
  • @Ed : Ohh missed that line, was just focusing on the difference between the two snippets. you are absolutely right of that lead to an issue. – sumandas Aug 17 '16 at 16:07
  • 1
    #2: i know if the array has never held elements, it will work fine first pass, but a 2nd pass through, it won't update with new rows. – TWL Aug 17 '16 at 16:23
  • 1
    @TWL wow, very interesting. I was skeptical when I read this, but I think http://stackoverflow.com/questions/15422120/notifydatasetchange-not-working-from-custom-adapter is exactly what you are describing. – trooper Aug 17 '16 at 16:32
  • I know, I don't have a real explanation behind why it is, just that it works as I and many others have bumped into this problem before. – TWL Aug 17 '16 at 16:36
  • @TWL:Can you please elaborate? I didn't understand what you mean – Jim Aug 17 '16 at 17:02
  • it has something to do with `=` is unable to connect to the same object, so the old `this.dataset` never gets the new rows. thats why #1 is used, most commonly in updating listviews (#2 will bug out). – TWL Aug 17 '16 at 17:37

2 Answers2

0

i found answer on Doc Oracle,
+ addAll(Collection c): Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator + this.dataset = rows: just tell data in dataset will remove and fill all data from "rows" into dataset.

atimetoremember
  • 133
  • 3
  • 12
0

The difference between addAll() and the variable assignment is that addAll() according to the docs:

Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator.

This means should a List be non-null, any objects added using the addAll() method would be appended to the end.

The variable assignment on the other hand would replace the current List stored in this.dataset with the List rows. This would therefore not append anything to the previous List.

However, as the method uses this.dataset.clear() which according to the docs:

Removes all of the elements from this list. The list will be empty after this call returns.

The functionality of the two methods is the same, as the list is empty before being appended to or overwritten.


Please note, to avoid a NullPointerException it would probably be best to add some sort of if-statement to check if this.dataset is null before calling this.dataset.clear();

In the second example, the this.dataset.clear() is also redundant, as the variable is overwritten immediately afterwards.

Ed Holloway-George
  • 5,092
  • 2
  • 37
  • 66
  • So the fact that we instantiate the adapter with a reference and change the reference has no impact on the notify? – Jim Aug 17 '16 at 17:00
  • Can you rephrase that @Jim? Not sure I am following you. If you are saying that your adapter is not refreshing, I think what you need to be doing is changing the dataAdapter's _inner_ dataset, it looks like you aren't doing that at present. – Ed Holloway-George Aug 17 '16 at 19:21
  • I am asking if the fact that we replace the reference instead of adding/modifying items from the list that the adapter had a reference can have any subtle problems – Jim Aug 17 '16 at 20:05