Background Knowledge
The ListView is widely used in Android applications, the necessary micro-blogs on mobile phones, NetEase news, etc., all using the ListView.
definition
A ListView is a collection of views that shows a list of scrollable items (view Group). Get content from a content source (such as an array or database query) through an adapter (Adapter)
Each item is then converted to a view that is rendered in the list, and the list item is automatically inserted into the list.
Demo
The implementation process of the ListView:
Prepare data----> Save data with dynamic array-----> Build Adapter (Adapter)-----> Add Adapter to the ListView and display
Mainactivity Code
1 PackageCom.johntsai.listviewdemo;2 3 Importjava.util.ArrayList;4 ImportJava.util.HashMap;5 Importjava.util.List;6 ImportJava.util.Map;7 8 Importandroid.app.Activity;9 ImportAndroid.os.Bundle;Ten ImportAndroid.view.View; One ImportAndroid.widget.AdapterView; A ImportAndroid.widget.AdapterView.OnItemClickListener; - ImportAndroid.widget.ListView; - ImportAndroid.widget.SimpleAdapter; the ImportAndroid.widget.Toast; - - Public classMainactivityextendsActivity { - PrivateListView ListView; + PrivateSimpleadapter Adapter; - //Preparing Data + Privatestring[] ListTitle = { A"2015 Year Spring Festival train ticket for sale today", at"Maldives flooded with freshwater crisis in China", -"China's 5 warplanes leap through the ancient Straits of Japan" - }; - Privatestring[] Listcontent = { -"Available network, phone ticket purchase, 3 hours can try \" Leackage check \ "", -"Freshwater production plants are damaged, freshwater supplies are in danger, and 150,000 of residents face water shortages", in"On 6th, leap to the western Pacific to train the ancient Straits of Miyako." - }; to PrivateList<map<string,object>>ListItems; + //Save Data - Private voidGetData () { theListItems =New *Arraylist<map<string,object>>(); $ for(inti = 0; i < listtitle.length; i++){Panax NotoginsengMap<string,object> ListItem = - NewHashmap<string, object>(); theListitem.put ("image", r.drawable.ic_launcher); +Listitem.put ("title", Listtitle[i]); AListitem.put ("Content", Listcontent[i]); the Listitems.add (listItem); + } - } $ @Override $ protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); the - GetData ();WuyiListView =(ListView) Findviewbyid (R.id.listview); the //Build Adapter - //First parameter: context, which is the context with which the view of the adapter runs Wu //second parameter: Data - //Third parameter: resource, which defines the layout ID of the view for the ListView item About //Fourth parameter: from $ //The fifth parameter: To, the ID of the view to display to -adapter =NewSimpleadapter ( This, - ListItems, - R.layout.listview_item, A NewString[] {"Image", "title", "Content"}, + New int[] {r.id.list_image,r.id.item_title,r.id.item_content}); the //Add adapter to ListView and display - Listview.setadapter (adapter); $ the //to add a response listener event for a ListView item theListview.setonitemclicklistener (NewOnitemclicklistener () { the the @Override - Public voidOnitemclick (adapterview<?>Parent, view view, in intPositionLongID) { theToast.maketext (mainactivity. This, the"The one you clicked is titled" +Listtitle[position], About Toast.length_long). Show (); the } the }); the + } -}
Mainactivity Interface Layout File
1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 android:orientation= "vertical">6 7 <ListView8 Android:id= "@+id/listview"9 Android:layout_width= "Match_parent"Ten Android:layout_height= "Wrap_content" One > A </ListView> - </LinearLayout>
ListView Item Layout File
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:id= "@+id/listviewitem"4 Android:layout_width= "Match_parent"5 Android:layout_height= "Match_parent"6 android:orientation= "Horizontal" >7 8 <ImageView9 Android:id= "@+id/list_image"Ten Android:layout_width= "50DP" One Android:layout_height= "50DP" A android:src= "@drawable/ic_launcher" - /> - <LinearLayout the Android:id= "@+id/list_content" - Android:layout_width= "Fill_parent" - Android:layout_height= "50DP" - android:orientation= "vertical" + > - <TextView + Android:id= "@+id/item_title" A Android:layout_width= "Match_parent" at Android:layout_height= "0DP" - Android:layout_weight= "1" - /> - <TextView - Android:id= "@+id/item_content" - Android:layout_width= "Match_parent" in Android:layout_height= "0DP" - Android:layout_weight= "1" to android:textsize= "10SP" + Android:textcolor= "@android: Color/darker_gray" - /> the </LinearLayout> * </LinearLayout>
Demo Run Effect:
The--listview of Android learning