This document defines a ListView through a custom list adapter, based on the listactivity.
Define the list item layout, including a picture display, title and description
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "80dip"> <ImageViewAndroid:id= "@+id/image"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" /> <LinearLayoutandroid:orientation= "vertical"Android:layout_width= "Wrap_content"Android:layout_height= "Fill_parent"Android:layout_marginleft= "10dip"Android:layout_marginright= "10dip"> <TextViewAndroid:id= "@+id/title"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" /> <TextViewAndroid:id= "@+id/description"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" /> </LinearLayout></LinearLayout>
In order for the view to display data, you must customize the adapter. ListView Each list item displays a custom model data, selecting inherit Baseadapter<t>,t as the custom model
Model class Content
Public classModel { Public stringName {Get; Set; } Public stringDescription {Get; Set; } Public intImage {Get; Set; } }
An adapter class needs to implement two methods and two properties:
The Count property, the This property of type T, the Getitemid method, and the GetView method.
The This property returns the object data corresponding to the specified index.
Custom Modellistadapter Code:
classModellistadapter:baseadapter<model> { PublicList<model>Models {Get; Set; } PublicModellistadapter (list<model>models) {Models=models; } #regionImplemented abstract members of Baseadapter Public Override LongGetitemid (intposition) { returnposition; } Public OverrideView GetView (intposition, View Convertview, ViewGroup parent) {
Gets the object that corresponds to the current position from the data sourcevaritem =Models [position]; Avoid repeatedly creating and destroying list item viewsif(convertview==NULL) {Layoutinflater inflater= Application.Context.GetSystemService ("Layout_inflater") asLayoutinflater; Convertview= Inflater. Inflate (Resource.Layout.CustomItem,NULL); } varImage = Convertview.findviewbyid<imageview>(Resource.Id.image); vartitle = Convertview.findviewbyid<textview>(Resource.Id.title); varDescription = convertview.findviewbyid<textview>(Resource.Id.description); Image. Setimageresource (item. Image); Title. Text=item. Name; Description. Text=item. Description; returnConvertview; } Public Override intCount {Get { returnModels.count; } } #endregion #regionImplemented abstract members of Baseadapter Public OverrideModel This[intIndex] { Get { returnModels [index]; } } #endregion }
The final step is to assign the liatactivity ListAdapter to our custom adapter
varModels =NewList<model>{ NewModel () {name="Name", description="Description", Image =Resource.Drawable.Icon},NewModel () {name="Name", description="Description", Image =Resource.Drawable.Icon},NewModel () {name="Name", description="Description", Image =Resource.Drawable.Icon},NewModel () {name="Name", description="Description", Image =Resource.Drawable.Icon},NewModel () {name="Name", description="Description", Image =Resource.Drawable.Icon},NewModel () {name="Name", description="Description", Image =Resource.Drawable.Icon}}; This. ListAdapter =NewModellistadapter (models);
Custom adapters, we can do more flexible processing. Do more in the GetView method, and if you just make a simple custom list style, you can quickly complete the operation by Simpleadapter.
ilist<idictionary<string,Object>> items =Newjavalist<idictionary<string,Object>> (); varItem1 =Newjavadictionary<string,Object> (); Item1. ADD ("name","Show Name"); Item1. ADD ("Description","Description"); Item1. ADD ("Image", Resource.Drawable.Icon); varITEM2 =Newjavadictionary<string,Object> (); Item2. ADD ("name","Show Name"); Item2. ADD ("Description","Description"); Item2. ADD ("Image", Resource.Drawable.Icon); Items. ADD (ITEM1); Items. ADD (ITEM2); This. ListAdapter =NewSimpleadapter ( This, Items,resource.layout.customitem,New string[]{"name","Description","Image"},New int[]{resource.id.title,resource.id.description,resource.id.image}];
Items for the defined data source K, define simpleadapter incoming parameters. The value defined in string[] must be equal to the value of key in dictionary, string[] and int[] two parameters represent from string[] to int[]. That is, the value in each string[] as the key, get the value in dictionary, assign to the view corresponding to the ID in int[].