Original: Uncle also said xamarin~android the click event in ~listview and get other elements of our bank
I am original, I contribute, I am the storage uncle
This article Uncle original, The spirit of the love of technology to study it, the results to share to the Chinese people! Uncle always believe in a word: You only choose a job of interest, you can better play your potential, and all this is based on your continuous research, continuous study under the premise.
Xamarin article on the Internet less, and the ListView related information better, a limited number of those are not clear, just tell everyone how to complete the button event, and in the event, how to get the other elements of the current line information is not stated, in the network for a long time, no harvest, Be sure to study it yourself, fortunately, and finally find a way to solve it.
Simple image
Look first, a ListView, its layout is a table, each row consists of several textview, and finally two button, we need to get the value of other elements on the corresponding row when we click each row button
A visit to native Android failed
What we can find on the web is the Android native Environment button implementation method, that it can be directly in the new View.onclicklistener this interface, and directly override the click Method, I sometimes do think Java syntax is strange, improper, The sense compiler should be for Onclicklistener this interface dynamically established an anonymous class, and then it overrides the click Method, after all, we think the interface can not be instantiated, hehe, and in Xamarin, how to rewrite, Also set up a class to inherit View.onclicklistener, say do, but unfortunately there is not a Onclicklistener interface in Xamarin, because of the reason of. NET naming specification, renamed this interface to Onclicklistener, I directly inherit it, real The click Method is now available, but it is possible that the Click event of the ListView has not been matched, so my research continues ...
Public classLvButtonListener:Android.Views.View.IOnClickListener { Public voidOnClick (View v) {V.findviewbyid<TextView> (Resource.Id.hello). Text ="Click"; Toast.maketext (V.context,"View OnClick2", Toastlength.short). Show (); } PublicIntPtr Handle {Get{return this. Handle; } } Public voidDispose () { This. Dispose (); } }
Continue to find information, according to adapter realization View.ionclicklistener, succeeded
The new class to implement the Ionclicklistener is not successful, so on the adapter directly to implement it, the code is also from the Internet, but only to say the penalty of the button, a few lines of code, does not show how to get the elements in the current line
/// <summary> /// Adapter /// </summary> Public class Task_infolistadapter:baseadapter<task_info>, Android.Views.View.IOnClickListener { // }
Implement the click Method, write your own logic, where the parameter view, which represents the view object where the current button is located, does not include elements from other rows, such as TextView, which you cannot find on this view object.
/// <summary> ///event when a button is clicked/// </summary> /// <param name= "V" >this chick .</param> Public voidOnClick (View v) {Switch(v.id) { CaseResource.Id.rechargeBtn:Toast.MakeText ( This. Context,"Recharge"+v.id, Toastlength.short). Show (); This. Context. StartActivity (typeof(rechargeactivity)); Break; CaseResource.Id.delBtn:Toast.MakeText ( This. Context,"Delete", Toastlength.short). Show (); Break; } }
Try more and find a solution eventually
The above code just tells us which button you clicked on, and cannot get the other elements of the current row of the button, so we use V.findviewbyid<textview> (Resource.Id.TaskID). Text This code to get the value is wrong, because as I have said above, the view object refers to the current button object, and not the whole line, after observing, found that the View object has a parent property, output it to find that it is our table row object, at this time, I came to spirit, see Hope, continue the code, list. Findviewbyid<textview> (Resource.Id.TaskID). Text, after testing found success, finally succeeded, not easy!
The following is the complete code for the adapter
/// <summary> ///Adapter/// </summary> Public classTask_infolistadapter:baseadapter<task_info>, Android.Views.View.IOnClickListener {/// <summary> ///data for all userinof/// </summary>List<task_info>items; Activity context; PublicTask_infolistadapter (Activity context, ienumerable<task_info>items):Base() { This. Context =context; This. Items =items. ToList (); } Public Override LongGetitemid (intposition) { returnposition; } Public OverrideTask_info This[intposition] { Get{returnitems[position];} } Public Override intCount {Get{returnitems. Count; } } /// <summary> ///The system will call and render. /// </summary> /// <param name= "position" ></param> /// <param name= "Convertview" ></param> /// <param name= "parent" ></param> /// <returns></returns> Public OverrideView GetView (intposition, View Convertview, ViewGroup parent) { varitem =Items[position]; if(Convertview = =NULL) { //using a custom userlistitemlayout, this is a template that can be traversed in the outer fabricConvertview = context. Layoutinflater.inflate (Resource.Layout.PartialCurrentTaskList,NULL); } Convertview.findviewbyid<TextView> (Resource.Id.TaskID). Text =item. Taskid.tostring (); Convertview.findviewbyid<TextView> (Resource.Id.ExpectedDate). Text =item. Createdate.tostring (); Convertview.findviewbyid<TextView> (Resource.Id.Status). Text =item. Status.tostring (); //get the button on the ListView varBTN = convertview.findviewbyid<button>(RESOURCE.ID.RECHARGEBTN); varDELBTN = convertview.findviewbyid<button>(RESOURCE.ID.DELBTN); //Click event on the button on the ListViewBtn. Setonclicklistener ( This); Delbtn.setonclicklistener ( This); returnConvertview; } /// <summary> ///event when a button is clicked/// </summary> /// <param name= "V" >this chick .</param> Public voidOnClick (View v) {//gets the current parent object varList = (v.parent asView); //other objects of the same sibling varTaskId = list. Findviewbyid<textview>(Resource.Id.TaskID). Text; Switch(v.id) { CaseResource.Id.rechargeBtn:Toast.MakeText ( This. Context,"Recharge"+v.id, Toastlength.short). Show (); This. Context. StartActivity (typeof(rechargeactivity)); Break; CaseResource.Id.delBtn:Toast.MakeText ( This. Context,"Delete TaskID:"+taskid.tostring (), Toastlength.short). Show (); Break; } } }
If you feel that this article is helpful to you, please help me to the top!
The study continues ...
Uncle also said xamarin~android the click event in ~listview and get other elements of our bank