Package org.hxsd.events;
Import static android.provider.basecolumns._id; Import static Org.hxsd.events.Constants.TABLE_NAME; Import static Org.hxsd.events.Constants.TIME; Import static Org.hxsd.events.Constants.TITLE; Import android.app.ListActivity; Import android.content.ContentValues; Import Android.database.Cursor; Import Android.database.sqlite.SQLiteDatabase; Import Android.os.Bundle; Import Android.widget.SimpleCursorAdapter;
public class Events extends Listactivity { Private Eventsdata events; Fields for queries private static string[] from = {_id, time, TITLE}; Sort private static String order_by = time + "DESC"; Show private static int[] to = {R.id.rowid, r.id.time, r.id.title};
/** called the activity is a. */ @Override public void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Events = new Eventsdata (this); try{ Add a record Addevent ("Hello, android!"); Get record information Cursor Cursor = GetEvents (); Display record information showevents (cursor); }finally{ Close Database Events.close (); } }
/** * Add data * @param string */ private void Addevent (string string) { Sqlitedatabase db = Events.getwritabledatabase (); Contentvalues values = new Contentvalues (); Values.put (Time, System.currenttimemillis ()); Values.put (TITLE, String); Db.insertorthrow (table_name, NULL, values); }
/** * Get Records * @return */ Private Cursor getevents () { Sqlitedatabase db = Events.getreadabledatabase (); Cursor Cursor = db.query (table_name, from, NULL, NULL, NULL, NULL, order_by); Startmanagingcursor (cursor); return cursor; }
/** * Show Records * @param cursor */ private void Showevents (Cursor Cursor) { /*stringbuilder builder = new StringBuilder ("Saved events:n"); while (Cursor.movetonext ()) { Long id = cursor.getlong (0); Long time = Cursor.getlong (1); String title = cursor.getstring (2); Builder.append (ID). Append (":"); Builder.append (Time). Append (":"); Builder.append (title). Append ("n"); } TextView TextView = (TextView) Findviewbyid (R.ID.TEXTVIEW01); Textview.settext (builder);
Simplecursoradapter adapter = new Simplecursoradapter (this, r.layout.item, cursor, from, to); Setlistadapter (adapter); } } Eventsdata.java documents are as follows: Package org.hxsd.events;
Import static android.provider.basecolumns._id; Import static Org.hxsd.events.Constants.TABLE_NAME; Import static Org.hxsd.events.Constants.TIME; Import static Org.hxsd.events.Constants.TITLE; Import Android.content.Context; Import Android.database.sqlite.SQLiteDatabase; Import Android.database.sqlite.SQLiteOpenHelper;
public class Eventsdata extends sqliteopenhelper{ Database name private static final String database_name = "EVENTS.DB"; Database version private static final int database_version = 2;
Constructors Public Eventsdata (context ctx) { Super (CTX, database_name, NULL, database_version); }
@Override public void OnCreate (Sqlitedatabase db) { Create data Table Events Db.execsql ("CREATE TABLE" + table_name + "(" + _id + "integer PRIMARY KEY autoincrement," + Time + "integer," + TITLE + ' TEXT not NULL '); }
@Override public void Onupgrade (sqlitedatabase db, int oldversion, int newversion) { If the version changes, delete the table events and then create a new Db.execsql ("DROP TABLE IF EXISTS" + table_name); OnCreate (DB); }
} |