. NET programmer play to Android Development---() ListView display Data

Source: Internet
Author: User

there are several controls for displaying data in Android, and this section lets us know that Listview,listview is the most commonly used data display control in Android, can display a simple data source, or can display a complex data source, the list items that we see in the Android system, It's basically a ListView credit. Data is displayed in the ListView and must be bound to the data source. Data source binding is done through adapter, there are two commonly used adapters in Android, Arrayadapter (array adapter) Simpleadapter (simple adapter), the role of the adapter is to display a complex data source to the Istview interface view, Is the bridge between the data source and the interface.

In this section we will recognize the two adapters, which are used by array adapters to display simple data, and simple adapters are primarily used to display complex data.

1. Array Adapter Arrayadapter

The array adapter displays the data relatively single, and we look at the following example

<?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=" wrap_content ">    </ListView></LinearLayout>


package Com.example.hellotest;import Android.app.activity;import Android.os.bundle;import Android.widget.ArrayAdapter;        Import Android.widget.listview;public class Firstlistview extends Activity {private ListView LV;   Private arrayadapter<string> adapter;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.firstlistview); lv= (ListView) Findviewbyid (R.ID.LISTVIEW1);//Get ListView object//listview data source string[] arr={"tour Cloud Stop Studio 1", "Tour Cloud Stop Studio 2", "        Arrayadapter demo "," Swim Cloud Stop Studio 207464864 "}; To initialize the adapter, parameter 1 is the context object, parameter 2 is the layout file for each list in the ListView, and Parameter 3 is the data source Adapter=new arrayadapter<string> (this,android.        R.layout.simple_list_item_1,arr); Lv.setadapter (adapter);//Bind data}} 


Let's analyze the parameters of the array adapter.

Adapter=new arrayadapter<string> (this,android. R.layout.simple_list_item_1,arr);

The first parameter is the current context object

The second parameter is the layout file, the system comes with the layout file that we used in the example

The third parameter is a data source

2. Simple Adapter Simpleadapter

Simple adapters are used to display complex data, so let's look at this example

First create a layout file for each item in the ListView Listitem.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=" 100DP "android:orientation=" vertical "and roid:gravity= "Top" > <linearlayout android:layout_width= "match_parent" android:layout_height= "Wrap_    Content "android:orientation=" horizontal "> <linearlayout android:layout_width=" match_parent " android:layout_height= "wrap_content" android:orientation= "Horizontal" android:layout_weight= "7" Android:gravit y= "center" > <imageview android:id= "@+id/pic" android:layout_width= "80DP" Android:layout_ height= "80DP" android:src= "@drawable/ic_launcher"/></linearlayout><linearlayout android:layout_width     = "Match_parent" android:layout_height= "wrap_content" android:orientation= "vertical" android:layout_weight= "3" > <textview AndroiD:id= "@+id/tvname" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:l ayout_margintop= "5DP" android:text= "Product Name:"/> <textview android:id= "@+id/tvprice" an        Droid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_margintop= "5DP" android:text= "Commodity price:"/> <textview android:id= "@+id/tvcolor" android:layout_width= " Wrap_content "android:layout_height=" wrap_content "android:layout_margintop=" 5DP "Android:layout_marg Inbottom= "5DP" android:text= "Product Color"/></linearlayout> </LinearLayout> <linearlayout Android:la Yout_width= "Fill_parent" android:layout_height= "2DP" android:background= "#F0F0F0" > </linearlayout></ Linearlayout>


Main Page Layout file

<?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/listview2" android:layout_width= "Match_        Parent "        android:layout_height=" wrap_content ">    </ListView></LinearLayout>


Package Com.example.hellotest;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.widget.arrayadapter;import Android.widget.listview;import Android.widget.simpleadapter;public class Simplelistview extends Activity {private ListView LV; Private Simpleadapter adp;//Define adapter Private list<map<string,object>> maplist;//define data source protected void OnCreate (Bundle savedinstancestate)        {super.oncreate (savedinstancestate);        Setcontentview (R.layout.simplelistview);        lv= (ListView) Findviewbyid (R.ID.LISTVIEW2);          /* Parameter 1 is the context object * Parameter 2 is the data source * Parameter 3 is the layout file * Parameter 4 is the key name * Parameter 5 is the binding layout file in the view ID *                * * */maplist=new arraylist<map<string,object>> ();        for (int i=0;i<10;i++) {map<string,object> map=new hashmap<string,object> ();    Map.put ("Pic", R.raw.pad);    Map.put ("name", "Product Name: Ipad Air");        Map.put ("Price", "Commodity prices: $" +i);        Map.put ("Color", "Product Colour: white");        Maplist.add (map); } adp=new Simpleadapter (this, Maplist,r.layout.listitem, new string[]{"pic", "name", "Price", "color"}, new int[]         {r.id.pic,r.id.tvname,r.id.tvprice,r.id.tvcolor});           Lv.setadapter (ADP); }}

Let's analyze the parameters of the simple adapter

Adp=new Simpleadapter (This, Maplist,r.layout.listitem, new string[]{"pic", "name", "Price", "color"}, new int[]{ R.id.pic,r.id.tvname,r.id.tvprice,r.id.tvcolor});

The first parameter is a context object

The second parameter is the data source, the type of the data source is the collection

The third parameter is the layout file for each item in the ListView

The fourth parameter is an array, each of which corresponds to the key name of the map in the data source

The fifth parameter is the ID of the corresponding control in the corresponding sub-layout file in the ListView

Download: http://download.csdn.net/detail/zx13525079024/8139657

. NET programmer play to Android Development---() ListView display Data

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.