Android Adapter Baseadapter Learning

Source: Internet
Author: User

Last night to see the next arrayadapter, and Simpleadapter, this morning up to watch the game, and continue to start learning, adapter in addition to the two front, there is a common is baseadapter, he is an abstract class. In fact, through the source can be seen, Arayaadapter and Simpleadapter are inherited from Baseadapter, the first two kinds of most just the information display, simple can be complex information display, and if you need to use the response, You can only customize the adapter. So we can also inherit baseadapter to implement the custom adapter

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; }}

Two Create activity
 Public classBaseadapteractivityextendsActivity {PrivateMyadapter Adapter; PrivateList<string>list; PrivateListView ListView; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_base_adapter); ListView=(ListView) Findviewbyid (R.ID.LISTVIEW4); List=NewArraylist<string>(); Adapter=NewMyadapter ( This, GetData ());    Listview.setadapter (adapter); } List<String>GetData () {List.add ("First line"); List.add ("Second Row"); List.add ("Line Three"); List.add ("Line Fourth"); returnlist; }} 

In order to find out, the online data check a lot, but fortunately it is finally achieved.

Android Adapter Baseadapter Learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.