Several methods of Android adapter

Source: Internet
Author: User

1 When was the ListView set for adapter data monitoring?

In Setadapter (ListAdapter adapter), the data listener in the original Madapter in the ListView is canceled first (Madapter.unregisterdatasetobserver ( Mdatasetobserver), and then set the adapter data listener for the new settings.

2 getView (int position, View Convertview, viewgroup parent)

We all know that the GetView method of Madapter is very important, so how is this method used in the ListView? In the ListView source code does not find the GetView method call, so we go to the ListView parent class Abslistview. GetView is called in Obtainview in Abslistview, and its main code logical part is:

1View Obtainview (intPositionBoolean[] isscrap) {2Isscrap[0] =false;3 View Scrapview;4         //get view from the collector5Scrapview =Mrecycler.getscrapview (position);6 7 View Child;8         if(Scrapview! =NULL) {9             ...Ten             //if not empty, the Convertview is passed in so that the view is reused and the data is updated OneChild = Madapter.getview (position, Scrapview, This); A             ... -}Else { -             //if empty, re-create the Holderview in GetView and fill in the data theChild = Madapter.getview (position,NULL, This); -             ... -         } -         returnChild ; +}

The Obtainview will be used in the measure of the ListView and in the production of the entire ListView.

The final thing to rewrite the GetView method is to remember the reuse of Convertview, which will almost always cause the memory to be unloaded.

3 GetCount ()

What is adapter's GetCount () used to do? In the ListView, the adapter GetCount () function is used in the onmeasure and in the touch distribution response. There is no doubt that it should return the data number of the underlying data.

4 getItem (int position)

GetItem () is called in Adapterview and is then called by the User: from the descriptions of these two functions we can see that we should return the position corresponding data in the adapter GetItem () method, But it doesn't mean that you have to return the data that is used for the view on item, which is still looking at the requirements, although most of the data that is displayed in the view may be returned.

    /*** Gets The data associated with the specified position in the list. *     * @paramposition which data to get *@returnThe data associated with the specified position in the list*/     PublicObject Getitematposition (intposition) {T Adapter=Getadapter (); return(Adapter = =NULL|| Position < 0)?NULL: Adapter.getitem (position); }        /**     * @returnthe data corresponding to the currently selected item, or * NULL if there are nothing selected. */     PublicObject GetSelectedItem () {T adapter=Getadapter (); intSelection =getselecteditemposition (); if(Adapter! =NULL&& adapter.getcount () > 0 && selection >= 0) {            returnAdapter.getitem (selection); } Else {            return NULL; }    }

Throughout the structure, it can be said that there are three layers: datalists (original underlying data)--adapter--adapterview, with the existence of the GetItem () method, we can use the adapter directly to obtain the data without the need to obtain the underlying datalists reference, and with the existence of the Getitematposition () method, we can directly utilize the Adapterview Gets the underlying data without needing to get a reference to its adapter. In this case, the simplicity of programming and decoupling is much better.

5 getitemid (int position)

some of its calls were found in Adapterview,

1      Public LongGetitemidatposition (intposition) {2T adapter =Getadapter ();3         return(Adapter = =NULL|| Position < 0)?INVALID_ROW_ID:adapter.getItemId (position);4     }5     6     7     Private voidfireonselected () {8         if(Monitemselectedlistener = =NULL)9             return;Ten  One         intSelection = This. Getselecteditemposition (); A         if(Selection >= 0) { -View v =Getselectedview (); -             //The return value of the Getitemid called here and selection belong to the same item, meaning that the onitemselected method of selecting the interface can be the             //to get the ID of the item directly, without having to get adapter to indirectly implement -Monitemselectedlistener.onitemselected ( This, V, selection, - Getadapter (). Getitemid (selection)); -}Else { +Monitemselectedlistener.onnothingselected ( This); -         } +     } A      at     intfindsyncposition () { -         ... -RowId =Adapter.getitemid (seed); -             if(rowId = = Idtomatch) {//from here, Getitemid seems to be able to return different values for different item, keeping the uniqueness -                 //Found It! -                 returnseed; in             } -         ... to}

As with the GetItem () method analyzed above, Getitemid () and getitemidatposition () provide the convenience of programming. But at the moment, because there is no need for the ID, so most of the rewrite Getitemid method is the direct return of the position value, this is also true, although from the data acquisition is not meaningful (I give you a position, you return to me intact, what meaning). But what I want to make out is not to be limited by this practice, but to think that Itemid is the item's position in the data. In fact, if there is a need, you can use the Getitemid () method to return some other values, such as each item data in the database ID value, or each person's ID number, etc.

Several methods of Android adapter

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.