Android Learning-View list (ListView and Listactivity)

Source: Internet
Author: User

Description

View Lists (ListView and Listactivity) are similar to AutoComplete, spinner, and they all require a list item for display, and you can provide display list items with the help of content adapter

There are two ways of creating a ListView:

(1) Create directly using the ListView

(2) Activity succession listactivity


Common XML attributes for a ListView


The following are two ways to create a ListView

Method One: Create directly using the ListView

(1) Main_activity.xml

Below is a layout of two ListView, an array-based, a adapter-based

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    android:id= "@+id/linearlayout"    android:orientation= "vertical"    android:layout_width= "fill_parent"    android:layout_height= "Fill_ Parent ">        <listview         android:id=" @+id/listview1 "        android:layout_width=" Match_parent        " android:layout_height= "Wrap_content"        android:entries= "@array/language"       />    <textview         Android:layout_width= "Fill_parent"        android:layout_height= "30DP"/>    <listview        android:id= "@ +id/listview2 "        android:layout_width=" fill_parent "        android:layout_height=" Wrap_content "        android: Headerdividersenabled= "true"        android:footerdividersenabled= "true"        android:divider= "@drawable/img02"/ >   </LinearLayout>

(2) Strings.xml storage ListView1 content Array

<?xml version= "1.0" encoding= "Utf-8"?><resources>    <string name= "App_name" >listview</ string>    <string name= "action_settings" >Settings</string>    <string name= "Hello_world" >hello world!</string>    <string-array name= "Language" >        <item> Chinese </item>        <item> Korean </item>        <item> english </item>        <item> Japanese </item>        <item> Portuguese </item>        <item> Russian </item>    </string-array></resources>



(3) Mainactivity.java

Steps:

1. Get Layout listview

2. package Display contents list or array

3. Build Adapter Adapter

4. Add an adapter to the ListView

Package Com.example.listview;import Java.util.arraylist;import Java.util.hashmap;import java.util.List;import Java.util.map;import Android.app.activity;import Android.os.bundle;import Android.util.log;import Android.view.view;import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.arrayadapter;import Android.widget.listview;public class Mainactivity extends Activity {private ListView listView2 = null; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main);//Get LISTVIEWLISTVIEW2 = (ListView) Findviewbyid ( R.ID.LISTVIEW2);//define array String arr[] = new string[]{"China", "Korea", "Japan", "United States", "Portugal", "Russia"};//Declaration Adapter//this  Context          //android.               r.layout.simple_list_item_checked list Style         //arr  display content (array or list collection)  arrayadapter<string > Arrayadapter = newArrayadapter<string> (this, Android. R.layout.simple_list_item_checked, arr);//listview Add Adapter Listview2.setadapter (Arrayadapter); Listview2.setonitemclicklistener (New Onitemclicklistener () {@Overridepublic void Onitemclick (adapterview<?> Parent, View view,int position, long id) {LOG.I ("ListView", parent.getitematposition (position). ToString ());}});}

The results are as follows:




If you want to customize the list, the list item displays multiple components. We can use Simpleadapter to customize our list.

(1) Main_activity.xml

Layout of the Imageview,textview component for displaying list contents

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:id= "@+id/linearlayout" android:orientation= "vertical" android:layout_width= "fill_parent" android:layout_height= "fill_parent" > <L Istview android:id= "@+id/listview" android:layout_width= "fill_parent" android:layout_height= "Wrap_con Tent "/> <linearlayout android:orientation=" Horizontal "android:layout_width=" Fill_paren T "android:layout_height=" fill_parent "> <imageview android:id=" @+id/img "android:layout _width= "50DP" android:layout_height= "65DP"/> <textview android:id= "@+id/name" android:layou T_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_marginleft= "100DP" Android : layout_margintop= "30DP"/> <textview android:id= "@+id/phone" android:layout_width= "Wrap_co Ntent "Android:layout_height= "Wrap_content" android:layout_margintop= "30DP" android:layout_marginleft= "150DP"/> </linea Rlayout></linearlayout>

(2) The Mainactivity.java step is the same as above

Package Com.example.listviews;import Java.util.arraylist;import Java.util.hashmap;import java.util.List;import Java.util.map;import Android.app.activity;import Android.os.bundle;import Android.view.menu;import Android.widget.listview;import Android.widget.simpleadapter;public class Mainactivity extends Activity {private ListView listview = NULL; Define the ListView component @overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);//Get ListView Component ListView = (ListView) Findviewbyid (R.id.listview);// Initialize the underlying data String name[] = new string[]{"Zhang San", "John Doe", "Harry", "Zhang Fei"}; String phone[] = new string[]{"14313426573", "15908263541", "18012345678", "13423456789"};int img [] = new int[]{ r.drawable.name01,r.drawable.name02,r.drawable.name03,r.drawable.name04};//encapsulates the list collection data list<map<string, object>> list = new arraylist<map<string,object>> (); for (int i=0;i<name.length;i++) {Map< string,object> ListItem = new hashmap<string,Object> (); Listitem.put ("img", Img[i]); Listitem.put ("name", Name[i]); Listitem.put ("Phone", phone[i]); List.add ( ListItem);} Configure Simpleadapter Adapter//this context//r.layout.activity_main layout file//new string[]{"img", "name", "Phone"} List contents key//new int[]{ R.id.img,r.id.name,r.id.phone} The layout component used to display the content simpleadapter Simpleadapter = new Simpleadapter (this, list, R.layout.activity_main,new string[]{"img", "name", "Phone"}, new Int[]{r.id.img,r.id.name,r.id.phone});// Add Adapter Listview.setadapter (Simpleadapter);}}

The results are as follows:



In addition to Simpleadapter, we can also override the Baseadapter GetView method as a list item for the listing. Interested students can write and write Baseadapter




Mode two: Activity succession listactivity


Attention:

The default layout of the listactivity consists of a full-screen list at the center of the screens. If you do not want to use the default layout, you can set your own layout by using the Setcontentview () method in the onCreate () method. If you specify your own custom layout, your layout must contain a ListView with an ID of "@id/android:list". If you also specify a view with an ID of "@id/android:empty", the view is displayed when no data is displayed in the ListView, and ListView will be hidden


(1) Main_activity.xml

The ID of a ListView and Textview,textview is laid out @id/android:empty, which is displayed when no data is displayed in the ListView .

<linearlayout  xmlns:android= "http://schemas.android.com/apk/res/android"    android:orientation= " Vertical "    android:layout_width=" fill_parent "    android:layout_height=" fill_parent ">    <listview         android:id= "@+id/android:list"        android:layout_width= "match_parent"        android:layout_height= "Match_ Parent "/>    <textview android:id=" @android: Id/empty "        android:layout_width=" Match_parent        " android:layout_height= "Match_parent"        android:background= "#FF0000"        android:text= "No data"/>    </LinearLayout>

(2) Mainactivity.java

Package Com.example.listactivity;import Android.app.listactivity;import Android.os.bundle;import Android.widget.arrayadapter;import Android.widget.listview;public class Mainactivity extends ListActivity {@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); String arr[] = new string[]{"China", "Korea", "Japan", "United States", "Portugal", "Russia"};//set adapter arrayadapter<string >  arrayadapter = New Arrayadapter<string> (This, Android. R.layout.simple_list_item_checked, arr);//Add Adapter Setlistadapter (Arrayadapter) to Listactivity;}}



If the ListView has no content, TextView is displayed. As follows





The effect of

     mode two is exactly the same, but one adds an adapter to the ListView component and one adds an adapter to the Listactivity class.

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.