Android Adapter Tutorial (i)

Source: Internet
Author: User

Recently in the process of developing Android found that the ListView is undoubtedly a regular use of the component, and the mention of the ListView has to mention the adapter, I intend to use a few examples, the more easily to everyone to explain carefully the use of the adapter, although this is not a profound problem, I still hope to bring you some gains, I am also a rookie, write bad or wrong place also please help point out. Enter the following text:

First Let's get to know the adapter:


What is an adapter? as the name implies, it is appropriate to make some data suitable for display on the view. The adapter is like a display that presents complex things in a way that people can accept. It is also possible to understand the adapter, the ListView through the adapter, to understand what we want to add to the ListView, and the way we @override in the adapter, is to let us write some basic content that we want to tell the ListView.

It can be said that the adapter is a bridge between the data and views , it is very important to learn the adapter.


Let's first understand the adapter from all aspects, and then look at some examples that I think should be able to master it completely!

How the adapter works:

How does the adapter handle the resulting data and show it out? In fact, it is very simple, the adapter it is also a class, in the class it is actually the parent class of these methods:

public int GetCount ()///number of rows to get data

Public Object getItem (int position)//Get a record of a row according to position

Public long Getitemid (intposition)//The ID of a record

And the most important:

Publicview GetView (intposition, View Convertview, ViewGroup parent)

//compared to several other methods this method is the most important, it explicitly defines how the adapter will display the data we fill, and in the custom adapter we usually write a layout file for it.

These in the last one or two examples will be carefully explained, we from the simple look: First look at the common adapter


There are three of our commonly used adapters, and of course we do not include custom adapters: These are the three arrayadapter,simpleadapter,simplecursoradapter, and they are all inherited from Baseadapter.

Below I will introduce each, we continue to learn adapter in specific applications:

Said adapter had to say the ListView, or use the ListView had to mention adapter

In Android development, the ListView is a more commonly used component that presents the content as a list and can be displayed adaptively based on the length of the data.

The display of a list requires three elements:

1. Listveiw The view used to display the list.

2. The adapter is used to map data to mediations on the ListView.

3. The data is specific to the string, picture, or base component that will be mapped.

Based on the list of adapter types, the list is divided into three types, arrayadapter,simpleadapter and Simplecursoradapter

One of the easiest arrayadapter to display is a single line of words. Simpleadapter has the best extensibility and can customize various effects. Simplecursoradapter can be considered as a simple combination of simpleadapter database, can be in the context of the contents of the database in the form of a list display

We start with the simplest ListView (Arrayadapter):

I'm going to put some examples of these adapters in one app, then upload them to my resources, and then share the links to everyone. So I'm going to jump to each example by a button in the mainactivity, and now let's write this demo out in a step-by-step.


Project started!


(1) First, a new app, named Adapterdemo, first add a button in the Activity_main.xml, a jump when the use.


(2) Then create a new class Arrayadapterdemo inherit from activity as our first small example of activity, @Override our onCreate method

Create a new XML file Arrayadapterdemo.xml as our layout file, which contains a text field and a ListView with the following code:

Arrayadapterdemo.xml:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "vertical" >    <textview        android:layout_width= "wrap_content"        android:layout_height= " Wrap_content "        android:text=" This is an example of Arrayadapter ">    </TextView>    <listview        android: Id= "@+id/arrayadapterdemolistview"        android:layout_width= "wrap_content"        android:layout_height= "Wrap_ Content ">    </ListView></LinearLayout>

(3) and then back to Arrayadapterdemo, define a listview called LV, and = (ListView) Findviewbyid (r.id. arrayadapterdemolistview );   , Setcontentview () is the XML layout just now, and then set an adapter for the LV, which uses Android native arrayadapter ( context  context, inttextviewresourceid,  list <T> objects) to assemble the data, To assemble the data, you need an adapter that connects the ListView object and array Data to fit the work.

the construction of Arrayadapter requires three parameters , in order: this, the layout file (note Here the layout file describes the layout of each row of the list, Android. R.layout.simple_list_item_1 is a system-defined layout file that displays only one line of text, a data source (a list collection). At the same time with Setadapter () finished Cheng with the final work.

Generally write a GetData () method as the last parameter, the final code is as follows:

Package Com.example.adapterdemo;import Java.util.arraylist;import Java.util.list;import android.app.Activity; Import Android.os.bundle;import Android.widget.arrayadapter;import Android.widget.listview;public class Arrayadapterdemo extends Activity {private ListView lv; @Overrideprotected void onCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.arrayadapterdemo); lv= (ListView) Findviewbyid ( R.id.arrayadapterdemolistview); Lv.setadapter (New Arrayadapter<string> (this,android. R.layout.simple_expandable_list_item_1, GetData ()));} Public list<string> GetData () {list<string> data = new arraylist<string> ();        Data.add ("test data 1");        Data.add ("Test Data 2");        Data.add ("test Data 3");        Data.add ("test data 4");                 return data;}}

Then register the activity in the Androidmanifest.xml and complete the button listening jump in the main interface.

here is the implementation result:


This is the end of the tutorial first! Write in an article in a short space too long, we also do not like to see.

The next explanation is:

Simplecursoradapter displays a list of phone contacts in the ListView.

Please continue to pay attention to!


Source I will be at the end of the last article to send up, I also write the blog side of the code, I think this may be more clear thinking.

I am also a student, the level is limited, please advise!


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.