. Net programmers play with Android Development --- (12) display data in ListView, androidlistview

Source: Internet
Author: User

. Net programmers play with Android Development --- (12) display data in ListView, androidlistview

There are multiple controls for displaying data in Android. In this section, we will learn about ListView. ListView is the most commonly used data display control in Android. It can display simple data sources or complex data sources, the list items we often see in the Android system are basically the credit of ListView. The data is displayed in ListView. You must bind the data source. The data source is bound by an Adapter. There are two commonly used adapters in Android: ArrayAdapter (array Adapter) SimpleAdapter (simple Adapter ), the adapter is used to display complex data sources to the istview interface view, which serves as a bridge between the data source and interface.

In this section, we will understand these two adapters. array adapters are used to display simple data. Simple adapters are mainly used to display complex data.

1. array adapter ArrayAdapter

The data displayed by the array adapter is relatively simple. Let's look at the example below.

<?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:orientation="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 the Listview object // listview data source String [] arr = {"Youxiang cloud stop Studio 1", "Youxiang cloud stop Studio 2 ", "ArrayAdapter demo of Yunxiang cloud stop Studio", "Yunxiang cloud Stop Communication Group 207464864"}; // initialize the adapter. Parameter 1 is the context object, parameter 2 is the layout file of each list in 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. In this example, the built-in layout file is used.

The third parameter is the data source.

2. Simple adapter SimpleAdapter

Simple adapters are used to display complex data. Let's look at this example.

 

First, create a layout file listitem. xml for each item in the LISTVIEW.

<? 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" android: 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: gravity = "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: layout_marginTop = "5dp" android: text = "item name:"/> <TextView android: id = "@ + id/tvprice" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_marginTop = "5dp" android: text = "product price:"/> <TextView android: id = "@ + id/tvcolor" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_marginTop = "5dp" android: layout_marginBottom = "5dp" android: text = "product color"/> </LinearLayout> <LinearLayout android: layout_width = "fill_parent" android: layout_height = "2dp" android: background = "# F0F0F0"> </LinearLayout>


 

Homepage 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:orientation="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; // defines the adapter private List <Map <String, Object> mapList; // define the 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 ID of the view image in the binding layout file ***/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", "product price: $" + I); map. put ("color", "product color: 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 the context object.

The second parameter is the data source. The data source type is set.

The third parameter is the layout file of each item in ListView.

The fourth parameter is an array. Each item in the array corresponds to the MAP key name in the data source.

The fifth parameter is the ID of the control corresponding to the Child layout file in ListVIew.

 

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

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.