13. Android's ListView Learning ·

Source: Internet
Author: User

1. Add the ListView (1) XML layout file to the XML file
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android" xmlns:tools= "Http://schemas.android.com/tools" android:layout_width= "match_parent" android:layout_height= "Match_ Parent "android:paddingbottom=" @dimen/activity_vertical_margin "android:paddingleft=" @dimen/activity_horizontal_ Margin "android:paddingright=" @dimen/activity_horizontal_margin "android:paddingtop=" @dimen/activity_vertical_ Margin "tools:context=". Mainactivity "> <listview android:id=" @+id/listview "android:layout_width=" Match_parent "an droid:layout_height= "Match_parent" android:entries= "@array/names" android:divider= "#aa0000" android:d ividerheight= "20DP" android:drawselectorontop= "false" android:scrollbars= "Horizontal|vertical" Androi D:fastscrollenabled= "true" android:listselector= "#00ffff" > </ListView> </RELATIVELAYOUT&G t; 
(2) String.xml
<?xml version= "1.0" encoding= "Utf-8"?><resources>    <string name= "App_name" >listviewdemo</ string>    <string name= "action_settings" >Settings</string>    <string name= "Hello_world" >hello world!</string>    <string-array name= "names" >        <item> Water electricians </item>        < item> </item>        <item> starving high </item>        <item> </item> <item>        Change </item>        <item> recommended </item>        <item> in-car </item>        <item> identity </item >        <item> Participation Awards </item>        <item> such as cry-eye </item>        <item> style </item>        <item> Rice Cooker </item>        <item> It's almost there. </item>        <item> wife </item>        <item> the wrong people </item>    </string-array></resources>
(3) Mainactivity
Package Com.example.listviewdemo;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.listview;import Android.widget.textview;import Android.widget.toast;public class MainActivity Extends Activity implements Onitemclicklistener{private ListView lv; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); LV = (ListView) Findviewbyid (R.id.listview); Lv.setonitemclicklistener (this);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}  @Overridepublic void Onitemclick (adapterview<?> arg0, View arg1, int arg2, long arg3) {//TODO auto-generated method StubSystem.out.println ("Parent =" + Arg0.getclass ()); System.out.printlN ("View =" + arg1); TextView TV = (TextView) arg1; Toast.maketext (This,tv.gettext (), Toast.length_short). Show (); System.out.println ("Position =" + arg2); System.out.println ("ID =" + Arg3);}}
2. Use listactivity and Arrayadaptersince listactivity is an activity in itself, only one space exists for the page, and the ID of the space is certain.
Import Android.app.listactivity;import Android.os.bundle;import Android.view.view;import Android.widget.arrayadapter;import Android.widget.listview;import Android.widget.textview;import Android.widget.toast;public class MainActivity2 extends listactivity{@Overrideprotected void OnCreate (Bundle Savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); String[] names = {"Zhang San", "John Doe", "Zhao Wu", "Wang Vi", "Qi Zhou", "Raise Bar", "Documentation",}; arrayadapter<string> adapter = new Arrayadapter<string> (this, Android. R.layout.simple_list_item_1,names); Setlistadapter (adapter);} @Overrideprotected void Onlistitemclick (ListView l, View v, int position, long ID) {//TODO auto-generated method Stubsupe R.onlistitemclick (l, V, position, id); TextView TV = (TextView) v; Toast.maketext (This,tv.gettext (), Toast.length_short). Show ();}}
3. Create a ListView using adapter to enable single and check(1) XML
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "vertical" >        <listview         android:id= "@+id/listview1" android:layout_width= "Match_        Parent "        android:layout_height=" match_parent "        >    </ListView></LinearLayout>
(2) Activity
Package Com.example.listviewdemo;import Android.app.activity;import Android.os.bundle;import Android.view.View; Import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.arrayadapter;import Android.widget.listview;public class MainActivity3 extends Activity implements  Onitemclicklistener{private ListView LV; @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate ( Savedinstancestate); Setcontentview (r.layout.main3); LV = (ListView) Findviewbyid (R.ID.LISTVIEW1); String[] Citys = {"Beijing", "Shanghai", "Guangzhou", "Shenzhen", "Toko", "China"};//Single mode//arrayadapter<string> adapter = new arrayadapter< String> (this, Android. R.layout.simple_list_item_single_choice,citys);//lv.setchoicemode (listview.choice_mode_single);// Multi-Select mode arrayadapter<string> adapter = new Arrayadapter<string> (this, Android. R.layout.simple_list_item_multiple_choice,citys); Lv.setchoicemode (listview.choice_mode_multiple); Lv.setOnItemCLicklistener (this);//Register Event Lv.setadapter (adapter);}  @Overridepublic void Onitemclick (adapterview<?> arg0, View arg1, int arg2, long arg3) {//TODO auto-generated method StubSystem.out.println ("View =" + Arg1);}}








13. Android's ListView Learning ·

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.