Uncle also said Xamarin ~ Android ~ The Click event in the ListView and obtain other elements of the row.
I am original, I contribute, I am an uncle warehouse
Uncle Ben is original. He studied it with his passion for technology and shared the results with Chinese people!Uncle always believes in one sentence: only by choosing a job you are interested in Can you better exert your potential, and all this is based on your continuous research and research.
Xamarin articles are rarely published on the Internet, while ListView provides better information. If there are only a few articles that are not clear, they just tell you how to complete the button event. In the event, there is no explanation of how to obtain other element information of the current row. I have been searching for it on the network for a long time and have not gained any benefits. Please study it by yourself. Fortunately, finally, we found a solution.
Image Summary
The layout of a ListView is a table. Each row consists of several textviews and the last two buttons. When we click the buttons on each row, you need to get the values of other elements on the corresponding row.
Failed to access native android
On the Internet, we can find the implementation method of buttons in the Android native environment, that is, it can be directly in the new View. the OnClickListener interface directly override the Click method. Sometimes, I think java syntax is strange and is not uniform. I feel that the compiler should dynamically create an anonymous class for the OnClickListener interface, then rewrite the Click method. After all, we think that the interface cannot be instantiated. In xamarin, how to rewrite the interface also creates a class to inherit the View. onClickListener, but unfortunately, there is no OnClickListener interface in xamarin, because. the reason for naming convention of. net is to rename this interface to OnClickListener. I directly inherit it and implement the Click method. However, it is possible that the click event of ListView is not correspondingly, my research continues...
public class lvButtonListener : Android.Views.View.IOnClickListener { public void OnClick(View v) { v.FindViewById<TextView>(Resource.Id.hello).Text = "click"; Toast.MakeText(v.Context, "View OnClick2", ToastLength.Short).Show(); } public IntPtr Handle { get { return this.Handle; } } public void Dispose() { this.Dispose(); } }
Continue to find information and press the Adapter to implement View. IOnClickListener.
The newly created class fails to implement IOnClickListener, so it is directly implemented on the adapter. This Code is also found online. However, I just mentioned the button penalty, A few lines of code does not explain how to obtain elements in the current row.
/// <Summary> /// adapter /// </summary> public class Task_InfoListAdapter: BaseAdapter <Task_Info>, Android. Views. View. IOnClickListener {// code}
Implement the Click method and write your own logic. The View parameter indicates the View object of the current button, which does not include elements in other rows, for example, TextView cannot be found on this View object.
/// <Summary> /// event when a button is clicked /// </summary> /// <param name = "v"> This girl presses </param> public void OnClick (View v) {switch (v. id) {case Resource. id. rechargeBtn: Toast. makeText (this. context, "recharge" + v. id, ToastLength. short ). show (); this. context. startActivity (typeof (RechargeActivity); break; case Resource. id. delBtn: Toast. makeText (this. context, "delete", ToastLength. short ). show (); break ;}}
Try more and finally find the solution
The code above only tells us which button you click, but cannot get other elements of the row where the current button is located. In this case, we use V. findViewById <TextView> (Resource. id. taskID ). it is wrong to use the Text code to obtain the value, because as I have already said above, the View object refers to the current Button object instead of the entire row. After observing, we found that the View object has a Parent attribute. After outputting it, we found that it is our table row object. At this moment, I came to the spirit and saw hope, continue code, list. findViewById <TextView> (Resource. id. taskID ). text. After testing, we found that it was successful and finally succeeded. It is not easy!
The complete code for the adapter is as follows:
/// <Summary> /// adapter /// </summary> public class Task_InfoListAdapter: BaseAdapter <Task_Info>, Android. views. view. IOnClickListener {// <summary> // All UserInof data // </summary> List <Task_Info> items; Activity context; public Task_InfoListAdapter (Activity context, IEnumerable <Task_Info> items): base () {this. context = context; this. items = items. toList ();} public override long GetItemId (int position) {return position;} public override Task_Info this [int position] {get {return items [position];} public override int Count {get {return items. count ;}/// <summary> // The system calls and render. /// </summary> /// <param name = "position"> </param> /// <param name = "convertView"> </param> /// <param name = "parent"> </param> // <returns> </returns> public override View GetView (int position, view convertView, ViewGroup parent) {var item = items [position]; if (convertView = null) {// use the custom UserListItemLayout, which is a template, it can be traversed externally. convertView = 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 (); // obtain the button var btn = convertView on the listview. findViewById <Button> (Resource. id. rechargeBtn); var delBtn = convertView. findViewById <Button> (Resource. id. delBtn); // click the event btn on the listView. setOnClickListener (this); delBtn. setOnClickListener (this); return convertView ;} /// <summary> /// event when a button is clicked /// </summary> /// <param name = "v"> This girl presses </param> public void OnClick (View v) {// obtain the current parent object var list = (v. parent as View); // other objects of the same Level var taskId = list. findViewById <TextView> (Resource. id. taskID ). text; switch (v. id) {case Resource. id. rechargeBtn: Toast. makeText (this. context, "recharge" + v. id, ToastLength. short ). show (); this. context. startActivity (typeof (RechargeActivity); break; case Resource. id. delBtn: Toast. makeText (this. context, "delete taskID:" + taskId. toString (), ToastLength. short ). show (); break ;}}}
If you think this article is helpful to you, please help me!
Study continues...