Objective
The listview is a control used to display a list of data, and today brings you how to use the cursor for data binding and click events.
Guide
1. How to create a ListView
2. How to bind data using the cursor
3.listview of Click events
Body
1. How to create a ListView
Here we customize a ListView view, first open Main.axml, drag a ListView and put it in.
Right-click Layout to create a new view, named Userlistitemlayout.axml, and drag two textview in
So we've finished a custom listview,
is to use the ListView to inherit the Listactivity class, you can not inherit, do not inherit and do not inherit the method, I will write two methods.
SQLite VDB; ICursor cursor; protected Override voidOnCreate (Bundle bundle) {Base. OnCreate (bundle); //Setcontentview (Resource.Layout.Main);Actionbar.setdisplayhomeasupenabled (true); VDB =NewSQLite ( This); Cursor= Vdb. Readabledatabase.rawquery ("SELECT * from TestTable",NULL); Startmanagingcursor (cursor); string[] name =New string[]{"name","Phone" }; int[] Phone =New int[] {Resource.Id.textName, Resource.Id.textPhone}; ListAdapter=NewSimplecursoradapter ( This, Resource.Layout.UserListItemLayout, cursor, name,phone); }
Here we bind the cursor to the data, how to create the database please refer to the YZF The sqliteopenhelper of Xamarin.android, which inherits Listactivity, cannot use Setcontentview.
How to use Setcontentview, we can not inherit the listactivity, just need to change the code so that both can
Public classactivity1:activity {SQLite vdb; ICursor cursor; ListView ListView; protected Override voidOnCreate (Bundle bundle) {Base. OnCreate (bundle); //Setcontentview (Resource.Layout.Main);Actionbar.setdisplayhomeasupenabled (true); ListView= findviewbyid<listview>(Resource.Id.listView1);VDB =NewSQLite ( This); Cursor= Vdb. Readabledatabase.rawquery ("SELECT * from TestTable",NULL); Startmanagingcursor (cursor); string[] name =New string[]{"name","Phone" }; int[] Phone =New int[] {Resource.Id.textName, Resource.Id.textPhone}; Listview. Adapter=NewSimplecursoradapter ( This, Resource.Layout.UserListItemLayout, cursor, name,phone); }
As follows
3.listview of Click events
Here the ListView Click event has two methods, the first is to inherit the method used by Listactivity, the second is not to inherit the use of listactivity.
Here we can directly rewrite the Onlistitemclick method to invoke the ListView click event
protectedoverridevoidintlong ID) { }
Or you use the second method
Thisnew eventhandler<adapterview.itemclickeventargs>(Listview_itemclick) ; void Listview_itemclick (object sender, Adapterview.itemclickeventargs e) { }
Finally, the following
Xamarin.android ListView binding Data and click events