Android adapter Learning

Source: Internet
Author: User

Android has many adapters. First, let's look at the inheritance structure of these adapters.

Among these adapters, baseadapter is the most used and most familiar. Let him go and start with arrayadapter.

1. arrayadapter

 

Public class arrayadapter extends baseadapter implements filterable

Class Overview

A listadapter that manages a listview backed by an array of arbitrary (any) objects. by default this class expects that the provided resource ID references a single textview. if you want to use a more complex layout, use the constructors that also takes a field ID. that field ID shocould reference A textview in the larger layout resource. however the textview is referenced, it will be filled with the tostring () of each object in the array. you can add lists or arrays of m objects. override the tostring () method of your objects to determine (OK) What text will be displayed for the item in the list. to use something other than textviews for the array display, for instance, imageviews, or to have some of Data besides tostring () results fill the views, overrideGetview (INT, view, viewgroup)To return the type of view you want.

A listadapter is used to manage a listview filled with an array of arbitrary objects. The default listadapter requires only one textview in the XML layout configuration file of each listview item. If you want to use a layout, you need to use the constructor containing the ID field, this ID is used to reference A textview in the Complex layout file. textview is referenced. The object in the array is used to call the tostring method and convert it to a string to fill in the textview, you can use an array or set containing custom objects. Override the tostring () method of the custom object to ensure that the listview is displayed. You can also use other non-textview controls to display data in the array, such as imageviews. You can override the getview method of the adapter to get the view you want.

Constructor:

Public arrayadapter (context, int textviewresourceid)

Context: The current context. Current context object

Textviewresourceid: The resource ID for a layout file containing a textview to use when instantiating views. the ID of a layout XML file containing textview. Note: (only one textview control is available in this layout file. textview cannot have a parent control; otherwise, an error occurs in Java. lang. illegalstateexception: arrayadapter requires the resource ID to be a textview)

Similar to this XML

<? XML version = "1.0" encoding = "UTF-8"?>
<Textview Android: Id = "@ + ID/subject"
Xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"
Android: layout_margintop = "5dip" Android: textappearance = "? Android: ATTR/textappearancemedium"
Android: singleline = "true" Android: ellipsize = "end"/>

Public arrayadapter (context, int textviewresourceid, t [] objects)

Objects: array used to fill the listview and provide data to the arrayadapter

Public arrayadapter (context, int textviewresourceid, list <t> objects) // we recommend that you use this function to directly populate arrayadapter with data.

//************************************** **************************************** *******************

Public arrayadapter (context, int resource, int textviewresourceid)

This is used for Complex layout. The layout file of the listview item contains more than one textview control.

Resource: The resource ID for a layout file containing a layout to use when instantiating views. xml file for the complex layout of item items in listview

Textviewresourceid: the ID of the textview within the layout resource to be populated (displayed) The textview used to display the data in the arrayadapter in the Complex layout XML file of the item in the listview

Public arrayadapter (context, int resource, int textviewresourceid, t [] objects) Public arrayadapter (context, int resource, int textviewresourceid, list <t> objects) // We recommend that you use this function to directly fill the arrayadapter with data.

Method:

Public static arrayadapter <charsequence> createfromresource (context, int textarrayresid, int textviewresid)

Creates a new arrayadapter from external resources. The content of the array is obtained throughGettextarray (INT). Parameters this method can use the data configured in the array XML file to create an arrayadapter. How to obtain the content in this array is obtained through this. getresources (). gettextarray (ID) method.

Textarrayresid: The identifier of the array to use as the data source. ID of the custom array XML file, that is, the data to be bound to the listview by arrayadapter

Textviewresid: The identifier of the layout used to create views. // ID of the layout file used to display array data (Note: Only one textview exists in the layout file. If there are multiple textviews, an error is reported, which is generally classcastexception)

Public View getdropdownview (INT position, view convertview, viewgroup parent)

GetViewThat displays in the drop down popup the data at the specified position in the data set.

Position: Index of the item whose view we want.

Convertview: the old view to reuse, if possible. note: You shoshould check that this view is non-null and of an appropriate type before using. if it is not possible to convert this view to display the correct data, this method can create a new view.

Parent: the parent that this view will eventually be attached

Returns
    • AViewCorresponding (corresponding) to the data at the specified 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.