ListView's adapter use of the beginner Arrayadapter string_android

Source: Internet
Author: User

ListView is something that is often used in Android, binding data for beginners, especially those who are just in contact with programming, often find it difficult to understand, when I was a sophomore to learn Java, but basically the equivalent of no learning, nothing written, Real touch programming is to start learning Android, the record down, you can look back, you can let the novice better understanding. The master bypassed ....

Android Adapter I understand that, is the bridge between data and view, the data is processed in adapter, and then displayed on the view.

There are many kinds of adapter, Arrayadapter<t>, Baseadapter, CursorAdapter, Headerviewlistadapter, ListAdapter, Resourcecursoradapter, Simpleadapter, Simplecursoradapter, Spinneradapter, Wrapperlistadapter.

What I've used in the project is Arrayadapter<t>, (arrays can also be arrays of objects), Baseadapter (base class for all adapter), Simpleadapter,cursoradapter (data source is cursor ), Simplecursoradapter, feel the need to sum up.

The most basic number to the SDK official website above gives the code example of the http://developer.android.com/resources/tutorials/views/hello-listview.html.

I wrote an example of my own, first the figure, and then put the code:



Copy Code code 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 the activity is a. */
@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);
/* We want to listview the data shown above, put 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 type string.
arrayadapter<string> arrayadapter = new arrayadapter<string> (//Arraylistdemo.this, Android. R.layout.simple_list_item_1,
Adapterdata);
/* Set ListView Adapter/listview.setadapter (new arrayadapter<string> (Arraylistdemo.this,
Android. R.layout.simple_list_item_1, Adapterdata)); }
}


Code written in the comments, I think there are several points to explain, are very basic, the master laughed at.

1. The role of the adapter is the bridge between the data and the view
2. This small example is to display an array, we use Arrayadapter, array adapters, data type <> is string type, data type can also be other, including object type
3. arrayadapter<string> arrayadapter = new Arrayadapter<string> (
Arraylistdemo.this, Android. R.layout.simple_list_item_1,
Adapterdata);
This code is the code that creates an array adapter with three parameters, the first argument is the context, the current activity, and the second parameter is a built-in layout in the Android SDK, which has only one textview, 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 the view, and the third parameter is the data we want to display. According to these three parameters, ListView will iterate through each of the data in the Adapterdata, read one, and show it to the layout of the second parameter, thus forming the ListView we see. Do not know just learn the classmate understand not ...

This article comes from the "Life Like Summer" blog

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.