ArticleDirectory
- Adapter
- Listadapter
- Spinneradapter
- Baseadapter
- Arrayadapter <t>
- Cursoradapter
- Resourcecursoradapter
- Simplecursoradapter
- Simpleadapter
- Wrapperlistadapter
- Headerviewlistadapter
Functions of adapter
The adapter is a bridge between the adapterview view and data. The adapter provides access to data and generates a view for each data item. The functions are shown in:
Inheritance structure of the adapter
Functions of each class Adapter
As the top-level basic interface of the inherited structure, the adapter defines the basic method to be implemented by the adapter:
Public interface adapter {// register an observer. It is notified when the data indicated by the adapter changes. datasetobserver is an abstract class and defines two methods: onchanged and oninvalidated void registerdatasetobserver (datasetobserver observer); // cancel registering an observer void unregisterdatasetobserver (datasetobserver observer); // the number of data items int getcount (); // return the data item object getitem (INT position) at the specified position; // return the ID of the data item at the specified position long getitemid (INT position ); // indicates whether the IDs of all data items are stable. By default If false is returned, it is unstable. If true is returned in cursoradapter, The _ id in cursor is a constant Boolean hasstableids (); // generate the corresponding view getview (INT position, view convertview, viewgroup parent) for each data item; // to avoid wasting memory for generating a large number of views, in Android, the view in adapterview is reusable. For example, if you have 100 data items to display, and your screen can only display 10 data items at a time, // only 10 views are generated. When you drag down to display 11th views, the reference of 1st views will be passed over, and the data in the update will be displayed again. That is to say, the view can be reused, but the data in the update view is used to display a new, if the type of a view is ignore_item_view_type, the view will not be reused static final int ignore_item_view_type = adapterview. item_view_type_ignore; // obtain the graph type int getitemviewtype (INT position) at the corresponding position; // The number of view types that can be returned by getview. (Headerviewlistadapter can contain headers and footers. getview can return headers, footers, and views in adapter //. However, the getviewtypecount function only calls the getviewtypecount function of the internal adapter, the view type in header and footer is ignored. Int getviewtypecount (); static final int no_selection = integer. min_value; Boolean isempty ();}
The adapter has two sub-interfaces: listadapter (list) and spinneradapter (drop-down list), which define only a few methods. In general, except the wrapperlistadapter interface and its implementation class only implement listadapter, both interfaces are implemented at the same time.
Listadapter
// Whether all items in listadapter are enabled, that is, whether all items are selectable and clickable public Boolean areallitemsenabled (); // whether the specified position item is enabled Boolean isenabled (INT position );
Spinneradapter
// Generate the public view getdropdownview (INT position, view convertview, viewgroup parent) for the corresponding position drop-down item );
Baseadapter
An abstract class, the basic implementation class of the adapter, is generally used as the base class of other implementation classes, at the same time, listadapter and spinneradapter are implemented, and some methods are provided by default:
Public abstract class baseadapter implements listadapter, spinneradapter {// provides some methods. When the data changes, call the registered datasetobserver callback function private final datasetobservable mdatasetobservable = new datasetobservable (); public Boolean hasstableids () {return false;} public void registerdatasetobserver (datasetobserver observer) {mdatasetobservable. registerobserver (observer);} public void unregisterdatasetobserver (datasetobserver observer) {mdatasetobservable. unregisterobserver (observer);} // view associated with the notification. The corresponding data has changed public void notifydatasetchanged () {mdatasetobservable. policychanged ();} public void policydatasetinvalidated () {mdatasetobservable. policyinvalidated ();} public Boolean areallitemsenabled () {return true;} public Boolean isenabled (INT position) {return true;} // implement public view getdropdownview (INT position, view convertview, viewgroup parent) {return getview (Position, convertview, parent);} public int getitemviewtype (INT position) {return 0;} public int getviewtypecount () {return 1 ;} public Boolean isempty () {return getcount () = 0 ;}}Arrayadapter <t>
The following is an SDK description:
A concrete baseadapter that is backed by an array of arbitrary 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 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, override getview () to return the type of view you want.
By default, the view generated by arrayadapter expects a textview, or you can specify a layout and the ID of one of the resources whose type is textview. The data under it can be of any type, however, the tostring () method is called during display. That is to say, only textview can be used to display text. To display other data, you must override getview ()
Arrayadapter has five constructors.
Public arrayadapter (context, int textviewresourceid) {Init (context, textviewresourceid, 0, new arraylist <t> ();} public arrayadapter (context, int resource, int textviewresourceid) {Init (context, resource, textviewresourceid, new arraylist <t> ();} public arrayadapter (context, int textviewresourceid, t [] objects) {Init (context, textviewresourceid, 0, arrays. aslist (objects);} public arrayadapter (context, int resource, int textviewresourceid, t [] objects) {Init (context, resource, textviewresourceid, arrays. aslist (objects);} public arrayadapter (context, int textviewresourceid, list <t> objects) {Init (context, textviewresourceid, 0, objects );} public arrayadapter (context, int resource, int textviewresourceid, list <t> objects) {Init (context, resource, textviewresourceid, objects );}
There are a maximum of four parameters, including the current context, layout (saving), textview ID and data (which can be an array or list). In the constructor, a function called init is called.
Private void Init (context, int resource, int textviewresourceid, list <t> objects) {mcontext = context; minflater = (layoutinflater) context. getsystemservice (context. layout_inflater_service); mresource = mdropdownresource = resource; mobjects = objects; mfieldid = textviewresourceid ;}
Assign values to private fields in the class, and minflater is layoutinflater to generate views of corresponding items.
Class has two fields to save data
Private arraylist <t> moriginalvalues; private list <t> mobjects;
Here, moriginalvalues is used to save the pre-filtered data when filtering data and store the filtered data into mobjects.
The add, insert, remove, and clear functions are also defined in the arrayadapter to change data, and a Boolean variable mnotifychange is defined to indicate whether to notify the view after the data is changed using these functions (calls yydatasetchanged, when this function is called, mpolicychange is set to true.
Implementation of some functions:
Public int getcount () {return mobjects. size ();} public t getitem (INT position) {return mobjects. get (position);} public int getposition (T item) {return mobjects. indexof (item);} public long getitemid (INT position) {return position;} public view getview (INT position, view convertview, viewgroup parent) {return createviewfromresource (Position, convertview, parent, mresource);} public view getdropdownview (INT position, view convertview, viewgroup parent) {return createviewfromresource (Position, convertview, parent, mdropdownresource);}
we can see that both getview and getdropdownview generate views by calling createviewfromresourse.
Private view createviewfromresource (INT position, view convertview, viewgroup parent, int Resource) {view; textview text; If (convertview = NULL) {view = minflater. inflate (resource, parent, false);} else {view = convertview;} Try {If (mfieldid = 0) {// if no custom field is assigned, assume the whole resource is a textview text = (textview) view;} else {// otherwise, find the textview field within the layout text = (textview) view. findviewbyid (mfieldid);} catch (classcastexception e) {log. E ("arrayadapter", "you must supply a resource ID for a textview"); throw new illegalstateexception ("arrayadapter requires the resource ID to be a textview", e );} t item = getitem (position); If (item instanceof charsequence) {text. settext (charsequence) item);} else {text. settext (item. tostring ();} return view ;}
In createviewfromresource, first determine whether convertview exists. If it does not exist, inflate one, and then determine whether mfieldid is 0. If it is 0, the resource ID passed to arrayadapter is textview, otherwise, a layout is passed, and mfieldid is the ID of textview in layout. Then, getitem is used to obtain the data item at the corresponding position and determine whether it is a charsequence instance. If it is settext directly, or its tostring () function is called, The arrayadapter can only set a string for textvext by default, to use other views, You need to reload getview or getdropdownview. In general, it inherits the baseadapter to customize its own adapter.
There is also a static function in arrayadapter.
Public static arrayadapter <charsequence> createfromresource (context, int textarrayresid, int textviewresid) {charsequence [] strings = context. getresources (). gettextarray (textarrayresid); return New arrayadapter <charsequence> (context, textviewresid, strings );}
Read the character array defined in the resource file to generate arrayadapter as the data. You can see that only textview views can be used. Instead of a layout, you can specify the ID of a textview in layout.
An arrayfilter is also defined in the arrayadapter, inherited from the filter, used to filter data items (when the listview has a focus, filter the list items by entering characters on the keyboard ), the filtering method is to pass in a charsequence and determine whether the corresponding data item starts with this charsequence. If not, separate the data items with spaces to determine whether the separated strings start with this charsequence, if it is reserved (if the data is not charsequence, its tostring () is called ()). If the input charsequence is null or the length is 0, it is not filtered.
Cursoradapter
Used to display data in cursor.
In the constructor, you can pass the autorequery parameter to indicate whether to automatically call the requery () of the cursor when the data of the cursor changes to keep the View data up-to-date.
This class overwrites hasstableids () and returns true.
Public Boolean hasstableids () {return true ;}
In cursoradapter, the rewritten getview and getdropdownview determine whether the input convertview is null. If it is null, newview () or newdropdownview () is called accordingly to generate a view, while newdropdownview () there is only one statement return newview (context, cursor, parent); therefore, newview () is called at the end, newview () is abstract, and must be rewritten by a subclass.
After a view is generated through newview (), BindView (v, mcontext, mcursor) is called; the data in cursor is bound to the view generated by newview, this method is also abstract.
Cursoradapter implements the method in the interface cursorfilter. cursorfilterclient
// Change the public void changecursor (cursor) pointed to by cursor // convert cursor to charsequence and return "" Or call cursor. tostring () Public charsequence converttostring (cursor) // filter data public cursor runqueryonbackgroundthread (charsequence constraint)
Resourcecursoradapter
As shown in the class name, this class inherits from cursoradapter and generates views through XML. This class simply overrides some functions and converts XML to view through layoutinflater. inflate.
@ Override public view newview (context, cursor, viewgroup parent) {return minflater. inflate (mlayout, parent, false) ;}@ override public view newdropdownview (context, cursor, viewgroup parent) {return minflater. inflate (mdropdownlayout, parent, false );}
Simplecursoradapter
A simple implementation class of resourcecursoradapter that maps the corresponding columns in cursor to textview and imageview in the XML-defined view.
This class defines an Interface
Public static interface viewbinder {Boolean setviewvalue (view, cursor, int columnindex );}
This method is used to map columns in cursor to views.
The BindView is rewritten in simplecursoradapter to control the binding of cursor to the view. Its definition is as follows:
@ Override public void BindView (view, context, cursor) {final viewbinder binder = mviewbinder; Final int COUNT = MTO. length; Final int [] From = mfrom; Final int [] to = MTO; For (INT I = 0; I <count; I ++) {final view v = view. findviewbyid (to [I]); If (V! = NULL) {Boolean bound = false; If (binder! = NULL) {bound = binder. setviewvalue (v, cursor, from [I]);} If (! Bound) {string text = cursor. getstring (from [I]); If (text = NULL) {text = "";} If (V instanceof textview) {setviewtext (textview) V, text );} else if (V instanceof imageview) {setviewimage (imageview) V, text);} else {Throw new illegalstateexception (v. getclass (). getname () + "is not a" + "view that can be bounds by this simplecursoradapter ");}}}}}
First, check whether the private domain mviewbinder in the class is null (default: NULL, you can use setviewbinder). If it is not null, binder is used. setviewvalue (v, cursor, from [I]); is bound. If this function returns true, the binding is successful. If it returns false, it is bound by the simplecursoradapter rule, check whether the view is textview or imageview. If it is set to textview, an exception is thrown.
As you can see, We can customize a class to implement simplecursoradapter. viewbinder, and then use setviewbinder to change the BindView result.
Simpleadapter
A baseadapter implementation class is used to bind data to an XML-defined view. The data type is arraylist <Map <string,?>.
Simpleadapter also implements the filter interface for data filtering. The filtering method is similar to arrayadapter, but its data type is Map <string,?>, To determine each item in the map, if any of the items meets the requirements, they will be retained.
Simpleadapter also uses the BindView function to bind data. Like simplecursoradapter, simpleadapter also defines the same internal interface viewbinder. in BindView, first determine whether viewbinder is set through setviewbinder, if setviewvalue is set, setviewvalue is called for data binding. If its setviewvalue is not set, false is returned, and the following processing is performed: Check whether the view is checkable or textview, imageview and perform corresponding processing. It can be seen that simpleadapter also processes textview and imageview by default. Of course, setviewbinder can be used.
Wrapperlistadapter
It inherits from the listadapder interface, so it is also a listadapter. At the same time, it is embedded with another listadapter. Only one function is defined for obtaining the embedded listadapter.
Public listadapter getwrappedadapter ();
Headerviewlistadapter
Inherited from wrapperlistadapter, which is used when the listview you use has header views or footer views. This class is designed as a base class and is generally not required. When addheaderview is called for a listview, it is automatically converted to headerviewlistadapter. Therefore, when using listadapter, pay attention to the following, its type may change.
Class defines two arraylists for saving the header and footer
Arraylist <listview. fixedviewinfo> mheaderviewinfos; arraylist <listview. fixedviewinfo> mfooterviewinfos;
These two fields are not null. In the constructor, if the parameters assigned to these two fields are null, they are assigned to the headerviewlistadapter. empty_info_list value, which is defined
Static final arraylist <listview. fixedviewinfo> empty_info_list = new arraylist <listview. fixedviewinfo> ();
The listview. fixedviewinfo is defined
Public class fixedviewinfo {public view; listadapter # getitem (INT)}. Public object data; Public Boolean isselectable ;}
This class redefined functions such as getcount and areallitemsenabled to include both mheaderviewinfos and mfooterviewinfos. Hasstableids, getitemviewtype, and getviewtypecount only consider the embedded listadapter.