android - Why use an additional List (or array) inside a custom-adapter? -
i have asked myself above question , did not come real solution far.
below can see two implementations of custom adapter listview holding strings. first implementation uses additional list of values inside adapter, done in many listview tutorials have seen. (e.g. vogella-listview-tutorial)
public class customadapter extends arrayadapter<string> { private arraylist<string> mlist; public customadapter(context context, list< string > objects) { super(context, 0, objects); mlist = objects; } @override public view getview(int position, view convertview, viewgroup parent) { string item = mlist.get(position); // ... stuff ... return convertview; } } the second implementation below uses list of strings has been handed adapter, , not need additional list. instead, adapters getitem(int pos) method used:
public class customadapter extends arrayadapter<string> { public customadapter(context context, list< string > objects) { super(context, 0, objects); } @override public view getview(int position, view convertview, viewgroup parent) { string item = getitem(position); // ... stuff ... return convertview; } } now question:
what reason using additional list inside customadapter when can use adapters getitem(int pos) method , spare additional list?
since extending arrayadapter extends baseadapter not need have copy of dataset, providing super enough. if extends adapter, baseadapter have override
getcount()getitem(int)getview(int position, view convertview, viewgroup parent)getitemid(int)
all of require access dataset.
Comments
Post a Comment