Simple use of the ListView for Android Learning notes

Source: Internet
Author: User

(1)

<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/listview1 "        android:layout_width=" Match_parent "        android:layout_height= "wrap_content"        android:layout_alignparentleft= "true"        android:layout_ Alignparenttop= "true" >    </ListView></RelativeLayout>

(2)

Package Com.example.listview1;import Java.util.arraylist;import java.util.list;import android. R.anim;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.listadapter;import Android.widget.listview;import Android.widget.toast;public class Mainactivity extends Activity {private ListView listview1;private arrayadapter< String> arrayadapter; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); listView1 = (ListView) This.findviewbyid ( R.ID.LISTVIEW1)///Set optional, can also be set to Radio Choice_mode_single, the bottom of simple should also correspond to Listview1.setchoicemode (Listview.choice_mode_ multiple)///Set listView1 check mode, corresponding to Simple_list_item_multiple_choice below arrayadapter = new Arrayadapter<string> ( Mainactivity.this,android. R.layout.simple_list_item_multiple_choice,getdatasource ()); Listview1.setadapter (arrayadapter);/* * LISTVIEW1 Click events */ Listview1.setonitemclicklistener (New Onitemclicklistener () {@Overridepublic void Onitemclick (adapterview<?> Parent, View view,int position, long id) {Toast.maketext (mainactivity.this, "-->>" + arrayadapter.getitem ( Position), Toast.length_short). Show ();}}); Public list<string> Getdatasource () {list<string> List = new arraylist<string> (), for (int i = 0; i < 30; i++) {List.add ("Jack" + i);} return list;} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true;}}


Simple use of the ListView for Android Learning notes

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.