Android ListView Simple Usage

Source: Internet
Author: User

The ListView is one of the more commonly used controls and is summarized here for easy reference.

The program effect is to implement a listview,listview with a title, content and picture, and add click and long press Response.


Steps:

1. First in Activity_main. A listview is defined in XML.

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" fill_parent "    android:layout_height=" fill_parent ">    <listview        android:id= "@+id/listview"        android:layout_width= "wrap_content"        android:layout_height= "Wrap_ Content "/></linearlayout>
2. Define the layout of each item in the ListView, implemented with Relativelayout: List_items.xml
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/    Android "Android:layout_width=" Match_parent "android:layout_height=" wrap_content "android:paddingbottom=" 4DP "        android:paddingleft= "12DP" android:paddingright= "12DP" > <imageview android:id= "@+id/itemimage" Android:layout_width= "50DP" android:layout_height= "50DP" android:layout_alignparentright= "true" Andro        id:paddingtop= "10DP"/> <textview android:id= "@+id/itemtitle" android:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:text= "TextView01" android:textsize= "20DP"/> <te Xtview android:id= "@+id/itemtext" android:layout_width= "match_parent" android:layout_height= "Wrap_con Tent "android:layout_below=" @+id/itemtitle "android:text=" TextView02 "/></relativelayout>
3. Finally call and join the listener inside the mainactivity:

Package Com.chay.listviewtest;import Java.util.arraylist;import Java.util.hashmap;import android.os.Bundle;import Android.app.activity;import Android.view.contextmenu;import Android.view.menuitem;import Android.view.View;import Android.view.contextmenu.contextmenuinfo;import Android.view.view.oncreatecontextmenulistener;import Android.widget.adapterview;import Android.widget.listview;import Android.widget.simpleadapter;import Android.widget.adapterview.onitemclicklistener;public class Mainactivity extends Activity {@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); /bind layout inside the Listviewlistview list = (ListView) Findviewbyid (R.id.listview);//Generate dynamic array, add data arraylistReference: www.iteye.com/topic/540423
Download: Listviewtest.rar

Android ListView Simple Usage

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.