HomeHealth basics for Android Projects 1: Baseadapter,

Source: Internet
Author: User

HomeHealth basics for Android Projects 1: Baseadapter,

When rewriting Baseadapter, we know that the following four methods need to be rewritten: getCount, getItem (int position), getItemId (int position), getView method,
GetCount determines the total number of items in listview, while getView returns the view displayed for each item.
But what is the role of getItem (int position) and getItemId (int position? How can I rewrite it?

First, let's look at getItem:

@ Override
Public Object getItem (int position ){

....
}

The official explanation is to Get the data item associated with the specified position in the data set. That is, to Get the data item at a specific position in the corresponding dataset. So where is this method called? When will it be called?

By checking the source code, it is found that the getItem method is called not in the Baseadapter class, but in the Adapterview.

In the adapterView class, we find the following method,

public Object getItemAtPosition( int position) {        T adapter = getAdapter();        return (adapter == null || position < 0 ) ? null : adapter.getItem(position);    }

So when is getItemAtPosition (position) called? Answer: it will not be automatically called. It is used to set

SetOnItemClickListener, setOnItemLongClickListener, and setOnItemSelectedListener

To obtain the data of the current row. Official Description: Impelmenters can call getItemAtPosition (position) if they need to access the data

Associated with the selected item. Therefore, we can write as follows:

@ Override
Public Object getItem (int position ){

Return this. datalist. get (position );

}

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

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

public long getItemIdAtPosition( int position) {        T adapter = getAdapter();        return (adapter == null || position < 0 ) ? INVALID_ROW_ID : adapter.getItemId(position);    }

Different getItem methods (such as onclicklistener's onclick method) have the id parameter, which depends on the returned value of getItemId.

We can generally achieve this:

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

Related Article

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.