Android adapter tutorial (1)

Source: Internet
Author: User

Android adapter tutorial (1)

Recently, when developing Android, I found that ListView is undoubtedly a frequently used component, but I have to mention the adapter when it comes to ListView. I plan to use several instances, from a simple perspective, I will explain the use of the adapter carefully. Although this is not a profound question, I still hope to bring you some gains. I am also a cainiao, please help us to find out what is not well written or wrong. Enter the text below:

FirstLet's get to know the adapter:


What is an adapter?As the name suggests, it is to make some data suitable for display on The View. An adapter is like a monitor that presents complex things in a human-acceptable manner. You can also understand the adapter. The ListView uses the adapter to understand what we want to add to the ListView, And the @ Override method in the adapter, let's write down some basic content that we want to tell this ListView.

It can be said thatThe adapter serves as a bridge between data and views.It is very important to learn the adapter well.


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

How the adapter works:

How does the adapter process the obtained data and display it? Actually it is very simple. To put it bluntly, the adapter is also a class. In the class, it implements the methods of the parent class:

Public int getCount () // get the number of rows of data

Public Object getItem (int position) // records a row based on position

Public long getItemId (intposition) // ID of a record

There are also the most important:

PublicView getView (intposition, View convertView, ViewGroup parent)

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

These will be explained in detail in the last two examples. Let's start with a simple look: first look at the commonly used adapters


There are three commonly used adapters. Of course, they do not contain custom adapters: ArrayAdapter, SimpleAdapter, and SimpleCursorAdapter. They all inherit from BaseAdapter.

Next I will introduce them one by one. We will continue to learn the Adapter in specific applications:

The Adapter has to say ListView, or the ListView has to mention the Adapter.

In android development, ListView is a commonly used component that displays specific content in the form of a list and can be automatically displayed based on the length of data.

The display of the list requires three elements:

1. ListVeiw is used to display the View of the list.

2.AdapterIt is used to map data to the mediation on ListView.

3. The specific string, image, or basic component of the data to be mapped.

Based on the list of adapter types, the list is divided into three types: ArrayAdapter, SimpleAdapter, and SimpleCursorAdapter

ArrayAdapter is the simplest and can only display one line of words. SimpleAdapter has the best scalability and Can Customize various effects. SimpleCursorAdapter can be considered as a simple combination of SimpleAdapter on the database. It can display the database content in the form of a list.

We start with the simplest ListView (ArrayAdapter ):

I plan to put the examples of these adapters in an app, upload them to my resources, and share the links with you. So I plan to jump to each example by using the Button in MainActivity. Now let's write this Demo step by step.


Project started!


(1) first, create an app named AdapterDemo. Add a button in activity_main.xml for later jump.


(2) create a new class ArrayAdapterDemo inherited from Activity as the Activity in our first small example. @ Override ourOnCreateMethod

Create an xml file arrayadapterdemo. xml as our layout file, which contains a text field and a ListView. The Code is as follows:

Arrayadapterdemo. xml:
 
     
      
      
      
  
 

(3) return to ArrayAdapterDemo and define a Listview named lv, and lv = (ListView) findViewById (R. id.Arrayadapterdemolistview);, SetContentView () is the xml layout just now, and then sets an adapter for lv. Here we use Android nativeArrayAdapter(ContextContext, inttextViewResourceId,List Objects) To assemble the data, you need an adapter that connects the ListView view object and the array data to adapt the two.

The construction of ArrayAdapter requires three parameters.The layout file (note that the layout file here describes the layout of each row in the list, android. r. layout. simple_list_item_1 is a layout file defined by the system that only displays one line of text, a data source (a List set ). At the same time, use setAdapter () to complete the final work of adaptation.

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
 
  
(This, android. R. layout. simple_expandable_list_item_1, getData ();} public List
  
   
GetData () {List
   
    
Data = new ArrayList
    
     
(); Data. add ("test data 1"); data. add ("Test data 2"); data. add ("Test data 3"); data. add ("test data 4"); return data ;}}
    
   
  
 

Register the Activity in AndroidManifest. xml and complete the button listener jump on the main interface.

The following is the implementation result:

I will post the source code at the end of the last article. I also write the code while writing a blog. I think this may give a clearer idea.

I am also a student and have a limited level. Please give me more advice!


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.