ArrayAdapter String for beginners of ListView Adapter

Source: Internet
Author: User

ListView is often used in Android. It is difficult for beginners to bind data, especially those who are new to programming. I learned java when I was a sophomore, but basically it is equivalent to having no learning or writing nothing. The real contact with programming is to start to learn about android. By recording these records, you can look back and give new users a better understanding. Expert bypass ....

In Android, Adapter is like this. It is a bridge between data and views. data is processed in the adapter and displayed on The View.

There are many types of adapters, including ArrayAdapter <T>, BaseAdapter, CursorAdapter, HeaderViewListAdapter, ListAdapter, ResourceCursorAdapter, SimpleAdapter, SimpleCursorAdapter, SpinnerAdapter, and WrapperListAdapter.

I have used ArrayAdapter in the project <T>, (arrays can also be object arrays), BaseAdapter (base class of all adapters), SimpleAdapter, cursorAdapter (data source is cursor), SimpleCursorAdapter, it is necessary to summarize.

The most basic code provided on the sdk official website is http://developer.android.com/resources/tutorials/views/hello-listview.html.

I wrote an example by myself. First, paste the Code:

Copy codeThe Code is as follows: package com. cz. list. demo;
Import android. app. Activity; import android. OS. Bundle;
Import android. widget. ArrayAdapter; import android. widget. ListView;
Public class ArrayListDemo extends Activity {
Private ListView listView; private String [] adapterData;
/** Called when the activity is first created .*/
@ Override public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState); setContentView (R. layout. array_list_layout );
/* Find This listView */
ListView = (ListView) findViewById (R. id. array_list );
/* Do not display any data on the listView, put it in an array */adapterData = new String [] {"Afghanistan", "Albania", "Algeria ",
"American Samoa", "Andorra", "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina", "Armenia ",
"Aruba", "Australia", "Austria", "Azerbaijan", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize ",
"Benin", "Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegovina", "Botswana", "Bouvet Island "};
/* This is an array of the string type */
// ArrayAdapter <String> arrayAdapter = new ArrayAdapter <String> (// ArrayListDemo. this, android. R. layout. simple_list_item_1,
// AdapterData );
/* Set the ListView Adapter */listView. setAdapter (new ArrayAdapter <String> (ArrayListDemo. this,
Android. R. layout. simple_list_item_1, adapterData ));}
}

There are comments written in the code. I think there are a few points to explain, which are all very basic and the experts will laugh.

1. the adapter serves as a bridge between data and views
2. in this small example, we want to display an array. We use ArrayAdapter, array adapter, and the Data Type <> which is String type, the data type of the data can also be of another object type.
3. ArrayAdapter <String> arrayAdapter = new ArrayAdapter <String> (
ArrayListDemo. this, android. R. layout. simple_list_item_1,
AdapterData );
This Code creates an array adapter with three parameters. The first parameter is the context, which is the current Activity, and the second parameter is a built-in layout in the android sdk, there is only one TextView in it. This parameter indicates that the layout of each piece of data in our array is this view, that is, each piece of data is displayed on this view; the third parameter is the data to be displayed. Based on these three parameters, listView traverses each piece of data in adapterData and reads one piece, which is displayed in the layout corresponding to the second parameter. This forms the listView we see. I don't know if my new students understand it...

This article is from the blog "Sheng Ru Xia Hua"

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.