One, implement the custom Myadapter
When inherited from Baseadapter, the compiler automatically prompts you for the method that needs to be implemented for that class, GetCount (), GetItem (), Getitemid (), GetView (). You only need to overload these abstract methods to implement functionality. Here is a small demo, used to bind a textview and a buttton. The first three overloaded functions are well understood and focus GetView. The first parameter, position, should be the index of the view, and the second parameter is not very clear at first, but it literally translates the view to see the great God tutorial on the web. is to turn a custom layout into a view, which uses a class like Layoutinflater. Really do not understand continue Baidu, as follows:
It is still very useful to layoutinflater this class in real-world development, which acts like Findviewbyid (). The difference is that Layoutinflater is used to find the XML layout file under res/layout/and is instantiated, while Findviewbyid () is looking for specific widget controls (such as button, TextView, and so on) under the XML layout file.
Specific role:
1, for a non-loaded or want to dynamically load the interface, you need to use Layoutinflater.inflate () to load;
2, for an already loaded interface, you can use the Activiyt.findviewbyid () method to obtain the interface elements.
In this case, it's much simpler, just get the view to the control and set the value for the control.
It is also not clear that the point of view, is to obtain the view, the first to determine whether Convertview is NULL, if NULL then create a view.
First to determine whether Convertview is null, when it is null, to recreate a view, not the case, directly with the Convertview. So what's this convertview thing? It is actually the view that just left the screen, so we can reuse it so that we can greatly reduce the creation of this view.
This can be said to greatly optimize the performance of this ListView, especially when we load the memory of the image of the resource, this effect is more obvious. But this is not enough, because after getting this view, each time still need to pass View.findviewbyid to get the control inside the view, Findviewbyid cost is also very high, then what is the method? In general, it is the application of Viewholder.
We can put these controls from view into a Viewholder object and set it to the Tag property of the view so that the next time you reuse the view, you don't have to Findviewbyid to get the control. Instead, you can get the corresponding control directly from the Viewholder.
Also add the corresponding function to the button
Public classMyadapterextendsbaseadapter{Privatecontext Context; PrivateList<string>list; PrivateLayoutinflater Minflater; PublicMyadapter (Context context,list<string>list) { This. Minflater =Layoutinflater.from (context); This. Context =context; This. List =list; } @Override Public intGetCount () {returnlist.size (); } @Override PublicObject GetItem (intposition) { returnList.get (position); } @Override Public LongGetitemid (intposition) { returnposition; } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {TextView TextView=NULL; Button btn=NULL; Viewhold Viewhold=NULL; if(convertview==NULL) {Viewhold=NewViewhold (); Convertview= Minflater.inflate (R.layout.baseitemlayout,NULL); Viewhold.textview=(TextView) Convertview.findviewbyid (R.ID.TEXTVIEW3); VIEWHOLD.BTN=(Button) Convertview.findviewbyid (R.id.button3); Convertview.settag (Viewhold); } Else{viewhold=(Viewhold) Convertview.gettag (); } viewHold.textView.setText (List.get (position)); ViewHold.btn.setText (button); ViewHold.btn.setOnClickListener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {toast.maketext (context,button, Toast.length_long). Show (); } }); returnConvertview; } Public classviewhold{ PublicTextView TextView; PublicButton btn; }}