Common usage of Android ListView (ii)

Source: Internet
Author: User

The ListView is a frequently used control, and each subkey inside the ListView item can make a string, or it can be a combined control. Let's talk about the ListView implementation:

1. Prepare the data for the ListView to be displayed ;

2. Using one-dimensional or multidimensional dynamic array save data;

2. Build adapter , < Span style= "font-size:medium;" To put it simply, adapter is Span style= "color: #008080;" >item array How many items are generated ;

3. Add the Adapter to the ListView and display it.

Next, you start the XML code for the UI:

The Main.xml code is as follows :

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutAndroid:id= "@+id/linearlayout01"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"xmlns:android= "Http://schemas.android.com/apk/res/android">                <ListViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/mylistview">        </ListView></LinearLayout>

The code for My_listitem.xml is as follows, My_listitem.xml is used to design the ListView item:

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutAndroid:layout_width= "Fill_parent"xmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_height= "Wrap_content"Android:id= "@+id/mylistitem"Android:paddingbottom= "3dip"Android:paddingleft= "10dip">        <TextViewAndroid:layout_height= "Wrap_content"Android:layout_width= "Fill_parent"Android:id= "@+id/itemtitle"android:textsize= "30dip">        </TextView>        <TextViewAndroid:layout_height= "Wrap_content"Android:layout_width= "Fill_parent"Android:id= "@+id/itemtext">        </TextView></LinearLayout>

Explain some of the properties that are used inside:

1.paddingbottom= "3dip", layout set aside 3 pixels of blank space at the bottom

2.paddingleft= "10dip", layout to left a blank area of 10 pixels

3.textsize= "30dip", TextView's font size is 30 pixels.

The last is the Java source code:

     Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.main); //The listview in the binding XML, as the container for itemListView list =(ListView) Findviewbyid (R.id.mylistview); //generate dynamic arrays, and reprint dataarraylistNewArraylist();  for(inti=0;i<30;i++) {HashMap<string, string> map =NewHashmap<string, string>(); Map.put ("Itemtitle", "This is the Title ..."); Map.put ("Itemtext", "This is text ...");        Mylist.add (map); }        //build Adapter, array = = = "ListItemSimpleadapter Mschedule =NewSimpleadapter ( This,//nothing to explain .MyList//Data SourcesR.layout.my_listitem,//XML implementation of ListItem //the children of the dynamic array corresponding to the ListItem                                                    NewString[] {"Itemtitle", "Itemtext"},                                                                                                         //two TextView IDs in the ListItem XML file                                                    New int[] {r.id.itemtitle,r.id.itemtext}); //Add and showList.setadapter (Mschedule); }

Common usage of Android ListView (ii)

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.