Android Basics-Build a universal adapter for rapid development

Source: Internet
Author: User

Here is a ListView demonstration, we are familiar with the ListView, the steps are divided into:

    • To create a Bean object for a ListView
    • Create a Itemview layout for the adapter of a ListView
    • Create a Adaoter for a listview (* * Focus)
    • Set Adapter to ListView

I. Create a Bean object for the ListView

Here is an example of student information

Second, create the Itemview layout of the adapter of the ListView

The final effect is

Iii. Creating a ListView Adaoter

Here is the official website of the adapter recommended wording, "You can find: Adapter cache is only the viewholder of each Itemview"

If we need to fill a lot of itemview, then the GetView () method inside the code will become bloated, difficult to read and modify, then we will beautify the code it

Iv. setting Adapter to the ListView

Let me have an obsessive-compulsive disorder, finish this.

Here we just see adapter code beautification, specifically see GetView () This method inside the content, said before, adapter cache is just Viewholder, then we extract this Viewholder

We extract the Findviewbyid () and Convertview.settag () from the Middle GetView (), and if you want to add a new view, just add it in the Viewholder class, and we can see that no matter how much is added to the view, In the GetView () method only one sentence is required, Viewholder Viewholder=getviewholder (Convertview)

From the above code analysis, what we need to do in our Omnipotent Viewholder class:

    • The Convertview cache is viewholder (so we need a convertview, as the Viewholder property)
    • Getviewholder code is fixed (so we need to provide a getviewholder () method to lock it)
    • Findviewbyid is executed every time the view is added (so we need to abstract a method to GetView ())

Below we create a Viewholder class (Viewholder simply understood as the manager of the view)

The Viewholder class did two things:

    • Getviewholder () Get this Viewholder object
    • Fill the view with data by using the Viewholder.getview () method

Universal adapter is very simple, is in the class with generic t to represent the Bean object passed in, the rest is to call viewholder things

We know Viewholder just do two things, then we can in adapter, IELTS application fee call these two things

We see the code in GetView () is still a lot, not beautiful, according to the idea of object-oriented, we can extract it as an abstract method, let our foreground to fill the view

At this time, the code in GetView () is only one sentence left.

Traditional use:

The use of universal adapter:

    • The two are better and worse, the traditional foreground code is concise, and the universal adapter code is bloated, but the universal adapter can be adapted to a variety of ListView and GridView
    • If you do not understand, the universal adapter code calls, a layer of the back piece, the final code is executed in the same order as the official website recommended, but he used generic T to make all objects applicable

Android Basics-Build a universal adapter for rapid development

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.