Getting Started with Android (12) List control basic use of 12.1 listview

Source: Internet
Author: User

Today, summarize the list controls in Android: ListView and Spinner.

ListView can display some information vertically and in a sliding way. The following explains how to display a series of information using ListView.

1. Create a project: BASICVIEWS5.

2. Code in the Strings.xml.

<?xml version= "1.0" encoding= "Utf-8"?>     
<resources>     
    <string name= "Hello" >hello world, basicviews5activity!</string>     
    <string name= "app_name" >BasicViews5</string>     
    < String-array name= "Presidents_array" >     
        <item>dwight D. eisenhower</item>     
        <item>john F. Kennedy</item>     
        <item>lyndon B. johnson</item>     
        <item>richard Nixon</item>     
        <item>gerald ford</item>     
        <item>jimmy carter</item>     
        <item>ronald Reagan</item>     
        <item>george H. bush</item>     
        <item>bill clinton</item>     
        <item>george W. bush</item>     
        <item>barack obama</item>     
    </string-array >             
</resources>

3. Code in the Basicviews5activity.java.

Package net.learn2develop.BasicViews5;     
Import android.app.ListActivity;     
Import Android.os.Bundle;     
Import Android.view.View;     
Import Android.widget.ArrayAdapter;     
Import Android.widget.ListView;     
         
Import Android.widget.Toast;     
             
    public class Basicviews5activity extends Listactivity {string[] presidents; /** called the activity is a.             
        * * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);     
        ---No need to call this---//setcontentview (R.layout.main);     
         
        Presidents = Getresources (). Getstringarray (R.array.presidents_array); Setlistadapter New Arrayadapter<string> (this, Android.     
    R.layout.simple_list_item_checked, Presidents)); } public void Onlistitemclick (ListvieW Parent, View v, int position, long id) {Toast.maketext (this, "for you have selected     
    "+ presidents[position], toast.length_short). Show (); }     
         
}

4. Press F11 to Debug.

5. Click on each line of information, there will be information pop-up.

First of all, it will be noted that the Basicviews5activity class is inherited listactivity. Listactivity is also inherited from the activity class, and ListView can display data from the bound data source. Therefore, it is not necessary to modify the Main.xml file to include a ListView, because Listactivity itself contains a listview. Then, there is no need to invoke Setcontentview () in the OnCreate () callback.

In the OnCreate () callback function, we use the Setlistadapter () method to manually let a ListView fill the entire activity.

The Arrayadapter class manages those displayed strings, and in this case we let ListView display the data using the Simple_list_item_1 method:

Setlistadapter New Arrayadapter<string> (this,     
            Android. R.layout.simple_list_item_checked, Presidents));

Finally, the Onlistitemclick () method is overridden, and the method is triggered when the data in the ListView is clicked:

public void Onlistitemclick (     
ListView Parent, View v., int position, long id)     
{     
    Toast.maketext (this,     
        " You are have selected "+ presidents[position],     
        toast.length_short). Show ();     

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/

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.