First, Introduction
No matter what software is developed, the use of the list is essential, and this chapter will learn how to use Xamarin to implement it, and how to use a custom adapter. The basics and adapters for the ListView in Xamarin can be viewed on the official website Https://developer.xamarin.com/guides/android/user_interface/working_ with_listviews_and_adapters/ This chapter mainly describes if you display multiple data in a ListView item.
Ii. preparatory work
1. Create an Android project named Mylistview
2. Create a new 2 view file
3. Edit our attempt below:
Copy and replace the following code into Stuadapter
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:minwidth= "25px"Android:minheight= "25px"> <LinearLayoutandroid:orientation= "Horizontal"Android:layout_width= "Match_parent"Android:layout_height= "30.5DP"Android:id= "@+id/linearlayout1"> <TextViewAndroid:text= "Name:"Android:layout_width= "Wrap_content"Android:layout_height= "Match_parent"Android:id= "@+id/textview1" /> <TextViewAndroid:text= "Text"Android:layout_width= "Wrap_content"Android:layout_height= "Match_parent"Android:id= "@+id/name" /> </LinearLayout> <LinearLayoutandroid:orientation= "Horizontal"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:id= "@+id/linearlayout2"> <TextViewAndroid:text= "Age:"Android:layout_width= "Wrap_content"Android:layout_height= "Match_parent"Android:id= "@+id/textview3" /> <TextViewAndroid:text= "Text"Android:layout_width= "Wrap_content"Android:layout_height= "Match_parent"Android:id= "@+id/age" /> </LinearLayout></LinearLayout>
View Code
Copy and replace the following code into Studentlist
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:minwidth= "25px"Android:minheight= "25px"> <ListViewAndroid:minwidth= "25px"Android:minheight= "25px"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:id= "@+id/stlist" /></LinearLayout>
View Code
4. Create a new activity named Studentslist, the new 2 classes are named info and Stuadapter, you can start coding below.
Third, binding data
1. First we need a container to store the data we need, so we add the following code to info
Public class Info:Java.Lang.Object { publicstringgetset;} // define student names Public int Get set; }// define Student age }
View Code
2. With the container, we now need to bind our data binding to the adapter, so we customize an adapter to add the following code
Public classStuadapter:baseadapter {List<Info>Stuitems; Activity Con; PrivateLayoutinflater Minflater; PublicStuadapter (Activity context,list<info>items) { This. Stuitems = items;//get the data passed in. This. Con =context; This. Minflater =Layoutinflater.from (context); } Public Override intCount {Get { returnStuitems.count; } } Public OverrideJava.Lang.Object GetItem (intposition) { returnStuitems[position]; } Public Override LongGetitemid (intposition) { return 0; } /// <summary> ///The most central approach/// </summary> /// <param name= "position" ></param> /// <param name= "Convertview" ></param> /// <param name= "parent" ></param> /// <returns></returns> Public OverrideView GetView (intposition, View Convertview, ViewGroup parent) {Stuview Stuview; if(Convertview = =NULL) {Stuview=NewStuview (); //The view where we want to fit the controlConvertview = Minflater.inflate (Resource.Layout.StuAdapter,NULL); //Bound ControlStuview.name = convertview.findviewbyid<textview>(Resource.Id.name); Stuview.age= convertview.findviewbyid<textview>(Resource.Id.age); //set the text to be displayed by the controlStuview.name.Text =Stuitems[position]. Name; Stuview.age.Text=Stuitems[position]. Age.tostring (); Convertview.tag=Stuview; } Else{Stuview=(Stuview) Convertview.tag; } returnConvertview; } //define a container to hold the control, note to inherit the Javaobject Public classStuView:Java.Lang.Object { PublicTextView name; PublicTextView age; } }
View Code
3. Below we can bind the list, add the following code to the Studentslist activity
Public classstudentslist:activity { PublicList<info> item;//Define a list PublicListView ListView;//Defining Controls PublicStuadapter adapter;//defining a data source protected Override voidOnCreate (Bundle savedinstancestate) {Base. OnCreate (savedinstancestate); Setcontentview (Resource.Layout.StudentsList);//set the view to displayadddate (); ListView= Findviewbyid<listview> (Resource.Id.StList);//Locate the controladapter =NewStuadapter ( This, item);//Binding AdapterListview. Adapter = Adapter;//set the data source for the ListView to adapter } /// <summary> ///Add Data/// </summary> Public voidadddate () {//Add three dataInfo info =NewInfo {Name ="Zhang San", age = + }; Info Info1=NewInfo {Name ="John Doe", age = - }; Info Info2=NewInfo {Name ="Wang er", age = - }; Item=NewList<info>(); Item. ADD (info); Item. ADD (INFO1); Item. ADD (Info2); } }
View Code
4. Finally, in our mainactivity activity, rewrite the button's click event to jump to the studentslist and OK.
New Intent (); Intent. SetClass (thistypeof(studentslist)); StartActivity (intent);
5.
Xamarin.android Getting Started: ListView and Adapter