Guide
1. Creation of views and databases
2.listview Data Binding
3.listview of Click events
Body
How to create a ListView, you can see here, the basic process operation is the same, I will not say, and then is a new database, the code is as follows
classSqlite:sqliteopenhelper { PublicSqlite (Context context):Base(Context,"notebooksql.db",NULL,1) { } Public Override voidOnCreate (Sqlitedatabase db) {db. Execsql ("CREATE TABLE notebooksql (_id INTEGER PRIMARY KEY autoincrement not null,title text not null,context text not Null,ti Me time not NULL)"); Db. Execsql ("INSERT into Notebooksql (title,context,time) VALUES (' This is the first note ', ' notes notes ' first note Note Note Notes First notes Note Notes first notes note notes first notes ', ' 2015-3-15 ')"); Db. Execsql ("INSERT into Notebooksql (title,context,time) VALUES (' This is the second note ', ' note Note ' Second Note Note Note Notes Second Note Note Note Second Note Note notes ', ' 2015-3-15 ')"); } Public Override voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) {db. Execsql ("DROP TABLE IF EXISTS notebooksql"); OnCreate (DB); } }
This sets up a self-increment primary key and three fields, and then I add two default data.
After the database is created we open Activity1, inherit listactivity, bind the data to the ListView
protected Override voidOnCreate (Bundle bundle) {Base. OnCreate (bundle); VDB=NewSqlite ( This); Cursor= Vdb. Readabledatabase.rawquery ("SELECT * from Notebooksql",NULL); Startmanagingcursor (cursor); string[] title =New string[] {the title"," Time" }; int[] Time =New int[] {Resource.Id.textView1, Resource.Id.textView2}; ListAdapter=NewSimplecursoradapter ( This, Resource.Layout.Item, cursor, title, time); }
Above
With the data of the ListView but can not operate there is no effect, we can re-Onlistitemclick method to add click events to the ListView
protected Override voidOnlistitemclick (ListView L, View V,intPositionLongID) {stringTitle= v.findviewbyid<textview>(Resource.Id.textView1). Text.tostring (); stringTime = v.findviewbyid<textview>(RESOURCE.ID.TEXTVIEW2). Text.tostring (); VDB=NewSqlite ( This); Cursor= Vdb. Readabledatabase.rawquery ("SELECT * from Notebooksql where title= '"+ title+"' and time = '"+ Time +"'",NULL); Cursor. Movetofirst (); stringNid = cursor. GetString (cursor. Getcolumnindex ("_id")); varIntent =NewIntent ( This,typeof(Note)); Intent. PutExtra ("ID", Nid); StartActivity (Intent); This. Finish (); }
This is convenient for the diagram, I get the ID directly according to title and time.
As follows
Xamarin.android Notepad (i)