Getting started with Android 2016 (8) -- ArrayAdapter of ListView

Source: Internet
Author: User

Getting started with Android 2016 (8) -- ArrayAdapter of ListView

ListView is a common control in Android.

What is a list view? Let's take a look at the figure first:

The most common example is the list of various menus.

To implement the list, you need to complete three elements:

1. ListView lists all data in the specified format. Each Item in the list can be called an Item (such as This is Title ). As you can imagine, to display the list, you must first make the corresponding format

2. the adapter is a format that can be recognized by ListView. Of course, there are several adapters, which will be detailed below. The adapter is data in the specified format, but the data from other sources in our database or network is not in this format. So we have the adapter intermediary.

3. Data to be displayed

Processing Procedure: Get Data = data is organized into a recognizable format, that is, adapter = put the adapter into ListView = show

There are three types of adapters: ArrayAdapter, SimpleAdapter, and SimpleCursorAdapter.

ArrayAdapter: array adapter, the simplest adapter. Only one line of words can be displayed.

Before reading the code, add some java knowledge. ArrayList is a dynamic array, which is equivalent to a vector of C ++.

Let's first look at the Code:

 

Package com. fable. helloworld; import android. app. activity; import android. OS. bundle; import android. widget. arrayAdapter; // array adapter package import android. widget. listView; // List View package public class HelloWorldActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {// data source, here is the test data List
 
  
Data1 = new ArrayList
  
   
(); Data1.add ("test1"); data1.add ("Test Data 2"); data1.add ("Test Data 3"); data1.add ("test data 4"); super. onCreate (savedInstanceState); // bind the ListView in XML as the Item container. ListView listView = new ListView (this); ArrayAdapter
   
    
Adapter = new ArrayAdapter
    
     
(This, android. R. layout. simple_expandable_list_item_1, data1); // Add and display listView. setAdapter (adapter); setContentView (listView );}}
    
   
  
 
The android. R. layout. simple_expandable_list_item_1 used above provides the layout file of the basic list items for Android.

The constructor ArrayAdapter used here (Contextcontext, int resource, List Objects ). The Context is the Context. Here is the current Activity, so this is passed in. Resource is the id of the resource file, which is recorded in R. java. Objects is the source data.

Integrate data with ArrayAdapter and put it into ListView to display the data.

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.