The adapter function is a bridge between the interface and the data, by setting the adapter to the ListView control (such as calling the ListView Setadapter (ListAdapter adapter)
), each item of the list is displayed to the page. In fact, when each item in the list is displayed to the page, the adapter GetView method is called to return a view, such as:
@Override
Public View GetView (int position, View Convertview, ViewGroup parent) {
return Super.getview (position, convertview, parent);
}
These two abstract methods are provided in CursorAdapter:
Makes a new view to the data pointed through cursor.
Public abstract View Newview (context context, cursor cursor, viewgroup parent);
Bind an existing view to the data pointed through cursor
public abstract void BindView (view view, context context, cursor cursor);
Newview and BindView refine the function implementation in the GetView, can be written in the GetView instead.
The order of the three calls is:
Getview-->newview-->bindview
Ext.: http://hi.baidu.com/spare_h/blog/item/a826d9d873f7053410df9b52.html
The principle of getview in the ListView
Basics of ListView and Adapter
Working principle:
The ListView requires adapter "Give me a View" (GetView) for each item in the list. A new view is returned and displayed
What if we have billions of items to show? Create a new view for each project? No!, this is impossible!
Android actually caches the view for you.
There's a widget called Recycler in Android, and that's how he works:
If you have 1 billion items (item), only the visible items exist in memory, others are in recycler. The ListView first requests a type1 view (GetView) and then requests other visible items. Convertview is empty (null) in GetView. When item1 rolls out of the screen and a new item comes up from the low end of the screen, the ListView requests a type1 view. Convertview is not a null value at this time, its value is item1. You just need to set up new data and return to Convertview, without having to recreate a view.
Take a look at the sample code below, where System.out is used for output in GetView
View Sourceprint?
01 |
Publicclassmultipleitemslist extendslistactivity { |
03 |
Privatemycustomadapter Madapter; |
06 |
Publicvoidoncreate (Bundle savedinstancestate) { |
07 |
Super.oncreate (savedinstancestate); |
08 |
Madapter = Newmycustomadapter (); |
09 |
for (inti = 0; i <; i++) { |
10 |
Madapter.additem ("item" + i); |
12 |
Setlistadapter (Madapter); |
15 |
Privateclassmycustomadapter Extendsbaseadapter { |
17 |
Privatearraylist mdata = Newarraylist (); |
18 |
Privatelayoutinflater Minflater; |
20 |
Publicmycustomadapter () { |
21st |
Minflater = (layoutinflater) getsystemservice (Context.layout_inflater_service); |
24 |
Publicvoidadditem (finalstring Item) { |
26 |
Notifydatasetchanged (); |
30 |
Publicintgetcount () { |
35 |
Publicstring GetItem (intposition) { |
36 |
Returnmdata.get (position); |
40 |
Publiclonggetitemid (intposition) { |
45 |
Publicview GetView (intposition, View Convertview, ViewGroup parent) { |
46 |
System.out.println ("GetView" + position + "" + Convertview); |
47 |
Viewholder holder = null; |
48 |
if (Convertview = = null) { |
49 |
Convertview = minflater.inflate (r.layout.item1, NULL); |
50 |
Holder = Newviewholder (); |
51 |
Holder.textview = (TextView) Convertview.findviewbyid (R.id.text); |
52 |
Convertview.settag (holder); |
54 |
Holder = (viewholder) convertview.gettag (); |
56 |
Holder.textView.setText (Mdata.get (position)); |
62 |
Publicstaticclassviewholder { |
63 |
Publictextview TextView; |
Execute the program, and then view the log in Logcat
GetView is called 9 times, Convertview is null for all visible items (see below)
View Sourceprint?
02-0513:47:32.559:info/system.out (947): GetView 0null |
02-0513:47:32.570:info/system.out (947): GetView 1null |
02-0513:47:32.589:info/system.out (947): GetView 2null |
02-0513:47:32.599:info/system.out (947): GetView 3null |
02-0513:47:32.619:info/system.out (947): GetView 4null |
02-0513:47:32.629:info/system.out (947): GetView 5null |
02-0513:47:32.708:info/system.out (947): GetView 6null |
02-0513:47:32.719:info/system.out (947): GetView 7null |
02-0513:47:32.729:info/system.out (947): GetView 8null |
Then scroll down the list slightly until ITEM10 appears:
Convertview is still null because there is no view in recycler (the edge of item1 is still visible at the top)
View Sourceprint?
02-0513:48:25.169:info/system.out (947): GetView 9null |
Scroll list again
Convertview is not a null value! Item1 left the screen to Recycler, and Item11 was created.
View Sourceprint?
02-0513:48:42.879:info/system.out (947): GetView [email protected] |
Scroll again:
View Sourceprint?
02-0514:01:31.069:info/system.out (947): GetView [email protected] |
02-0514:01:31.142:info/system.out (947): GetView [email protected] |
02-0514:01:31.279:info/system.out (947): GetView [email protected] |
02-0514:01:31.350:info/system.out (947): GetView [email protected] |
02-0514:01:31.429:info/system.out (947): GetView [email protected] |
02-0514:01:31.550:info/system.out (947): GetView [email protected] |
02-0514:01:31.669:info/system.out (947): GetView [email protected] |
02-0514:01:31.839:info/system.out (947): GetView [email protected] |
02-0514:03:30.900:info/system.out (947): GetView [email protected] |
02-0514:03:32.069:info/system.out (947): GetView [email protected] |
Convertview as we expected, after Item11 left the screen, its view (@437430f8) contained item21 as Convertview
ListView optimization
http://blog.csdn.net/yucf1988/article/details/6365297
Similarities and differences of GetView Newview BindView in CursorAdapter