"Android Personal Understanding (ii)" Learn how to work with custom adapters from an implementation approach

Source: Internet
Author: User

When customizing the adapter, you need to override Baseadapter's GetCount (), getItem (int position), getitemid (int position), and GetView () methods.
But you don't know how to rewrite four methods because you don't understand what each method does at work.
1. Understand the role of GetCount () and GetView () by the working principle of adapter.

GetCount () is used to tell the system the number of items, which is the number we are going to draw. So we rewrite this method:
public int GetCount () {
return datalist.length;
}
But when we write this in gallery:
public int GetCount () {
return integer.max_values;
}
Adding some other settings, there will be a reciprocating loop effect that the system will think we are going to draw 2 of the 31-time side of the item.
GetView () is the most important and well-known, and is not repeated here.
2, from the above, getItem (int position) and getitemid (int position) do not play a role in drawing the view, what is their role?
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 find that getitem (position) functions in the construction method of getitematposition (int position), returning data items at a specific location. As follows:
Public Object getitematposition (int position) {
T adapter = Getadapter ();
Return (adapter = = NULL | | Position < 0)? Null:adapter.getItem (position);
}
and the role of getitematposition is when we rewrite Setonitemclicklistener, Setonitemlongclicklistener, Setonitemselectedlistener, This method can be called to fetch the current row data, so it will not be automatically picked up by the system and will only be easily invoked by the developer at development time.
Sum up:
GetItem method: Obtain the data item at a specific location in the corresponding data collection, mainly implement the getitematposition (int position) method.
getitematposition (int position) method: Obtains the current row data, mainly facilitates the developer in Setonitemclicklistener, Setonitemlongclicklistener, Setonitemselectedlistener gets the data when it is written.
So in general, we can write this:
Public Object getItem (int position) {
return This.datalist.get (position);
}

Getitemid (int position):
Returns the ID of the postion corresponding item, and, like GetItem, puts the value of the ID back in the Onclik method that contains the ID parameter.
So in general, we can write this:
public long getitemid (int position) {
return position;
}
Finally, summarize the steps for customizing the adapter:
Declaring adapter-connected data
Four methods are implemented according to their respective functions, in which the View object is returned in GetView.

Add:
The meaning of Convertview in GetView is that when the ListView is sliding, the item is slid out of the screen and is no longer being used when Android will reclaim the view of this entry, which is the convertview here, The effect is to recycle the deprecated view object, reducing the number of times the view object is initialized. So we often:
Public View GetView (int position, View Convertview, ViewGroup parent) {
View view = null;
if (Convertview! = null) {
view = Convertview;
}
}

"Android Personal Understanding (ii)" Learn how to work with custom adapters from an implementation approach

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.