Xamarin.android ListView binding Data and click events

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.