android--List view (ListView)

Source: Internet
Author: User

List view is the most commonly used view component in Android, which lists the list items that need to be displayed in a vertical list. There are two ways to add a list view to the screen in Android: one is created directly using the ListView component, and the other is to have the activity inherit the listactivity implementation. These two methods are described below:

First, directly using the ListView component to create

Add a listview to the layout file first

The code is as follows:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"Xmlns:tools="Http://schemas.android.com/tools"Android:id="@+id/linearlayout1"Android:layout_width="match_parent"Android:layout_height="match_parent"android:orientation="Vertical"Tools:context=". Mainactivity"> <ListView Android:id="@+id/listview1"Android:layout_width="match_parent"Android:layout_height="wrap_content"android:entries="@array/cytpe"> </ListView></LinearLayout>

An array resource named CType is used here, so we want to create an XML file Arrays.xml code for a defined array resource in the Res/values directory as follows:

<?xml version= "1.0" encoding= "UTF-8"?>    <resources>        <string-array name= "Cytpe" >        < Item> profile </item>        <item> theme Modes </item>        <item> mobile </item>        <item> Program Management </item>        <item> call settings </item>        <item> connectivity </item>        </string-array >     </resources>

You can see the list view as shown below by running directly:

The following is an adapter to specify list items to create a ListView

Layout code:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:id=" @+id/linearlayout1 "    android:layout_width=" Match_parent    " android:layout_height= "Match_parent"    android:orientation= "vertical"    tools:context= ". Mainactivity ">    <listview        android:id=" @+id/listview1 "        android:layout_width=" Match_parent "        android:layout_height= "wrap_content"        android:divider= "@drawable/ic_launcher"        android: dividerheight= "3px"        android:footerdividersenabled= "false"        android:headerdividersenabled= "false"        >    </ListView></LinearLayout>

Java code:

Package Com.basillee.blogdemo;import Java.lang.annotation.retention;import Java.util.list;import android.os.Bundle ; Import Android.app.activity;import android.view.menu;import android.view.view;import Android.widget.AdapterView; Import Android.widget.adapterview.onitemclicklistener;import Android.widget.arrayadapter;import Android.widget.linearlayout;import Android.widget.listview;import Android.widget.toast;public class MainActivity Extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); ListView listview= (ListView) Findviewbyid (R.id.listview1); Arrayadapter<charsequence>adapter=arrayadapter.createfromresource (this,r.array.cytpe,android. R.layout.simple_list_item_single_choice); Listview.setadapter (adapter); Listview.setonitemclicklistener (new Onitemclicklistener () {@Overridepublic void Onitemclick (adapterview<?> parent, View arg1, int pos,long id) {String Result=parent.getitematpositIon (POS). toString (); Toast.maketext (Getapplicationcontext (), result, Toast.length_long). Show ();}});}}

Second, let the activity inherit listactivity realization:

If the program's window simply needs to display a list, it can be implemented directly with the activity inheriting listactivity. Instead of calling the Setcontentview method to display the page in a class that inherits Listactivity, you can set the adapter directly for it to display a list.

Just look at the code, and everyone knows it:

Package Com.basillee.blogdemo;import Java.lang.annotation.retention;import Java.util.list;import android.os.Bundle ; Import Android.app.activity;import android.app.listactivity;import android.view.menu;import Android.view.View; Import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.arrayadapter;import Android.widget.linearlayout;import Android.widget.listview;import Android.widget.toast;public class Mainactivity extends listactivity{@Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); String []cytpestrings=new string[]{"situational Mode", "Theme Mode", "Phone", "Program Management"}; Arrayadapter<string>adapter=new arrayadapter<string> (this, Android. r.layout.simple_list_item_single_choice,cytpestrings); Setlistadapter (adapter);} @Overrideprotected void Onlistitemclick (ListView l, View v, int position, long ID) {//TODO auto-generated method Stubsupe R.onlistitemclick (l, V, position, id); String Resultstring=l.getitematposItion (position). ToString (); Toast.maketext (Getapplicationcontext (), resultstring, Toast.length_long). Show ();}}

  

android--List view (ListView)

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.