The types and usage of adapter commonly used in Android

Source: Internet
Author: User

First, the commonly used adapter:

1, Baseadapter: The basic data adapter, its main purpose is to upload a set of data such as a ListView UI display components, inherited from the interface class adapter, because it is the basic type, so high degree of freedom, can be modified place more

2, Simpleadapter: Simple adapter, the system has customized some methods, you can override these methods

3, Arrayadapter: Data and UI one-to-one, incoming data source and layout file, complete display

4, Simplecursoradapter: Directional adapter, point to the database, you can easily put the contents of the database in the form of a list to display

Second, usage:

1, Baseadapter: Write their own constructs and methods, and then return the view to the list item binding in GetView

2, Simpleadapter:

Format: Simpleadapter (context context, List<? extends map< String,?>> data, int resource, string[] from, int[] C12>to)

Where: Context is the contextual, data refers to the map object array, resource refers to the layout file, from the data in the map object corresponding to the key name, to refers to the resource control ID

3, Arrayadapter:

Format:arrayadapter<string> (Context context, int resource, int Textviewresourceid , String[] objects)

Where: Context is contextual, resource refers to layout files, Textviewresourceid controls that bind data in layout files id,objects data array

4, Simplecursoradapter:

Format: Simplecursoradapter (context context, int layout, Cursor c, string[] from, int[] to, int flags)

Where: Context is contextual, layout file, C index according to Cusor,from refers to the database column name, to value bound control id,flags identity when the data changes call oncontentchanged (), Whether to notify ContentProvider that the data has changed

Third, optimize

Each adapter has a GetView method that returns the corresponding view-to-control, and if the method is replicated, it can return a custom view to the control display, for example:

@Override
Public View GetView (int position, view Convertview, ViewGroup parent)
{ null); ((TextView) Item.findviewbyid (R.id.showtext)). SetText (Data[position]); return item;}

Problem: The method above does not have a problem with a small amount of data, but a large amount of data, such as hundreds, consumes a lot of system resources

Workaround: Reduce View creation times

1, determine whether Convertview has been created, if created directly using the data binding

@override  Public View GetView (int  position, view Convertview, ViewGroup parent) {    ifnull)     {        null);     return
}

This reduces the number of views created and improves efficiency, but the binding of the data is still not efficient, so it is inefficient to use the method below to improve efficiency.

2, the use of Viewholder

Principle: The Binding object is defined in advance in viewholder, so that one binding can be used all the time without re-creation and binding, which improves efficiency compared to the above

classChatlistadapterextendsbaseadapter{
 Private classViewholder {TextView showtext; }
Various ellipsis
PublicView GetView (intposition, View Convertview, ViewGroup parent)  {Viewholder holder; if(Convertview = =NULL) {Convertview= Minflater.inflate (R.layout.list_item_layout,NULL); Holder=NewViewholder (); Holder.showtext=(TextView) Convertview.findviewbyid (R.id.showtext);
//Put holder into the tag of the view to facilitate the next direct reading, similar to the concept of cache Convertview.settag (holder); } Else {
//read out holder from tag to holder=(Viewholder) Convertview.gettag ();  } holder.text.setText (Data[position]); returnConvertview; }}

With the optimization of the big data, the system efficiency has been improved a lot

The types and usage of adapter commonly used in Android

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.