The role and rewriting of Baseadapter GetItem and Getitemid in Android

Source: Internet
Author: User

When rewriting baseadapter, we know that we need to rewrite the following four methods: Getcount,getitem (int position), getitemid (int position), GetView method,
GetCount determines how many items the ListView has, and GetView returns the view displayed for each item item.
But getitem (int position), what does getitemid (int position) do? How do you rewrite it?

First Look at GetItem:

@Override
Public Object getItem (int position) {

. ...
}

The official explanation is that get the data item associated with the specified position in the set. That is, you get the item in the corresponding data collection at a specific location. So where is the method called? When is it called?

By looking at the source code discovery, the GetItem method is not called in the Baseadapter class, but is called in Adapterview.

In the Adapterview class, we found the following method,

Public Object getitematposition (int position) {      T adapter = Getadapter ();      Return (adapter = = NULL | | Position < 0)? Null:adapter.getItem (position);  }

So when did getitematposition (position) be called? Answer: It will not be automatically called, it is used in our setting

Setonitemclicklistener, Setonitemlongclicklistener, and Setonitemselectedlistener are easily invoked in the click-to-Select handling event

To get the current row data. Official explanation Impelmenters can call getitematposition (position) if they need to access the data

Associated with the selected item. So in general, we can write this:

@Override
Public Object getItem (int position) {

return This.datalist.get (position);

}

Of course, if you like, you can also return null directly inside.


As for Getitemid(int position), it returns a similar method for the id,adapterview of the postion corresponding item:

public long getitemidatposition (int position) {      T adapter = Getadapter ();      Return (adapter = = NULL | | Position < 0)? INVALID_ROW_ID:adapter.getItemId (position);  }

Unlike GetItem, some methods (such as the OnClick method of Onclicklistener) have the ID parameter, which is determined by the return value of Getitemid ().

We can generally achieve this:

            @Override public            long getitemid (int position) {                return position;            }




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.