About Android Development Implementation List view

Source: Internet
Author: User

List view (ListView) lists the list items that need to be displayed in a vertical form.

There are two ways to add a list view to the screen in Android

1. Create directly with the ListView component

2, let the activity inherit listactivity realization

Just learn the first method and simply implement the use of this component

Layout file Code

1<?xml version="1.0"encoding="Utf-8"?>2<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"3Android:layout_width="match_parent"4android:layout_height="match_parent"5android:orientation="Vertical">6 7<ListView8Android:id="@+id/listview1"9android:entries="@array/list"          Ten        //Add the name of the array resource in the string.xml of Res/layout OneAndroid:layout_width="match_parent" Aandroid:layout_height="wrap_content"> -</ListView> -  the</LinearLayout> -     

Array resources in the corresponding res/layout

1<resources>2 3<stringName="app_name">xqx_lianxi</string>4<string-array name ="List">/The array resource name corresponds to the list and layout5<item> Shandong University </item>6<item> Shandong University of Science and Technology </item>7<item> Shandong University of Technology </item>8<item> Shandong University of Architecture </item>9<item> Shandong Agricultural University </item>Ten<item> Jinan University </item> One<item> Yantai University </item> A<item> Ludong University </item> -<item> Linyi University </item> -<item> Liaocheng University </item> the</string-array> -</resources>

Java code

1 Package xqx;2 3 import Com.example.xqx_lianxi. R;4 5 import android.app.Activity;6 import Android.os.Bundle;7 import Android.view.View;8 import Android.widget.AdapterView;9 import Android.widget.Toast;Ten import Android.widget.AdapterView.OnItemClickListener; One import Android.widget.ListView; A  -  Public classList_lianxi extends activity{ - ListView list;  the @Override -     protected voidonCreate (Bundle savedinstancestate) { -         //TODO auto-generated Method Stub - super.oncreate (savedinstancestate); + Setcontentview (R.layout.list_lianxi); -          +List =(ListView) Findviewbyid (r.id.listview1); A          at         //Add a response event for the selected item in the list view -List.setonitemclicklistener (NewOnitemclicklistener () { -  - @Override -              Public voidOnitemclick (adapterview<?> parent, View arg1,intPos, -                     LongID) { in                 //TODO auto-generated Method Stub -String result = Parent.getitematposition (POS). toString ();//gets the value of the selection toToast.maketext (List_lianxi. This,"clicked on the"+result, Toast.length_short). Show ();//output selected item message +             } -         }); the     } *      $}

About Android Development Implementation List view

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.