The ListView control is an entry container that displays each entry for a collection object (such as an array, list<t>, observablecollection<t>, etc.) and provides scrolling functionality.
The list view is the UI, the collection object is the data, the two certainly cannot be directly related, must use a transformer to implement the binding, this converter in the Android system is called the Adapter (adapter).
Android provides a number of predefined adapters for use, such as Arrayadapter (array adapters), which are inherited from Baseadapter to meet general requirements, but in most cases you need to define your own adapters for more versatility. The specific implementation is generally divided into the following steps:
1. Define the entity object that holds the data. Example: Defining an entity class Testentity
Public class testentity { publicintgetset;} Public string Get Set ; } }
2. Design a data template that will be applied to each entry item. Example: Create a new layout file under the resources/layout/folder named Testentitydatatemplate.axml
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "Horizontal"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"> <TextViewAndroid:text= "Text"Android:layout_width= "0DP"Android:layout_height= "Match_parent"Android:layout_weight= "1"Android:id= "@+id/testentityid" /> <TextViewAndroid:text= "Text"Android:layout_width= "0DP"Android:layout_height= "Match_parent"Android:layout_weight= "5"Android:id= "@+id/testentityname" /></LinearLayout>
3. Customize the adapter class.
Example: Define a class testadapter inherit from the Baseadapter<t> generic class and implement four abstract methods, ① indexer (GetItem method in non-generic class baseadapter). The ②count property. ③getitemid method. ④getview method. Where the GetView method returns a View object, which is called when the ListView is to display an entry item, and the View object returned by this method is the view of the entry item
Custom adapter classes typically require three objects, a context object, a resource ID value, an entry object
classTestadapter:baseadapter<testentity> { Privatecontext Context; Private intresourceId; Privatelist<testentity> items =NULL; PublicTestadapter (Context context,intResourceId, list<testentity>list) { This. Context =context; This. ResourceId =resourceId; This. Items =list; } Public OverrideTestentity This[intposition] { Get { return This. Items[position]; } } Public Override intCount {Get { return This. Items. Count; } } Public Override LongGetitemid (intposition) { returnposition; } Public OverrideView GetView (intposition, View Convertview, ViewGroup parent) {testentity Item=Items[position]; //increase operational efficiency if you do not have a view to create a new view if(Convertview = =NULL) { //instantiating layoutsConvertview = Layoutinflater.from ( This. Context). Inflate ( This. ResourceId,NULL); } Convertview.findviewbyid<TextView> (Resource.Id.TestEntityID). Text =item.id. ToString (); Convertview.findviewbyid<TextView> (Resource.Id.TestEntityName). Text =item. Name; returnConvertview; } }
4. So far, data classes, data templates, and adapter classes have been defined, and the next thing you need to do is create the appropriate objects and bind the ListView to the adapter.
Example: Creating a data collection and an adapter object, declared as a field of a class, for easy use in other methods
Private New List<testentity>(); Private null;
Set the Adapter property of the ListView to complete the binding
ListView listView1 = findviewbyid<listview>(Resource.Id.listView1); New Testadapter (This, Resource.Layout.TestEntityDataTemplate, testentitycollection); = Testadapter;
When the data collection changes, remember to call the adapter's notifydatasetchanged () method to notify the UI to update
Private void btnAdd_Click (object sender, EventArgs e) { int1; Testentitycollection.add (new testentity () { = ID, " test-" + ID. ToString () }); // testadapter.notifydatasetchanged (); }
Android--listview and data binding (Xamarin)