SQLite instance--dummynote.java

Source: Internet
Author: User
Tags date now

Attach the source code first. Code is constantly updated in the main implementation of a Notepad function, involving SQLite crud operations.


Dummynote.java


Package Com.example.dummynote;import Android.os.bundle;import Android.support.v4.widget.SimpleCursorAdapter; Import Android.view.menu;import android.view.menuitem;import Android. R.bool;import Android.app.listactivity;import Android.database.cursor;import Android.widget.arrayadapter;import Android.widget.listadapter;public class Dummynote extends listactivity {@Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); GetListView (). Setemptyview (Findviewbyid (R.id.empty));//The display contents of empty text box Setadapter () When the set list is blank;} Private string[] note_array={/* "Hangsome", "Lushichuanshuo", "Android", "Library" */};p rivate notesdbadapter mdbhelper ;p rivate Cursor mnotecursor;private void Setadapter () {mdbhelper = new Notesdbadapter (this); Mdbhelper.open (); Filldata ( );} private void Filldata () {mnotecursor = Mdbhelper.getall (); Startmanagingcursor (mnotecursor); String[] from = new string[]{notesdbadapter.note};int[] to = new int[]{android. R.id.text1};//andrOid. R.ID.TEXT1 is an identifier for the TextView inside the Android framework//now create a simple cursor Adaptersimplecursoradapter adapter =new Simplecursoradapter (this, Android. R.layout.simple_list_item_1, Mnotecursor, from, to); Setlistadapter (adapter);} ADD a entityprivate int mnotenumber = 1;protected static final int menu_insert = Menu.first; @Overridepublic boolean ONCR Eateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is present. super.oncreateoptions Menu (menu); Menu.add (0,menu_insert, 0, "Add Notes"); return Super.oncreateoptionsmenu (menu);} public boolean onoptionsitemselected (MenuItem item) {switch (Item.getitemid ()) {case menu_insert:string notename = "Note "+ mnotenumber++;mdbhelper.create (notename); Filldata (); return true;} return super.onoptionsitemselected (item);}} <p></p>
Notesdbadapter.java<pre name= "code" class= "java" >package com.example.dummynote;import Java.util.Date;import Android.content.contentvalues;import Android.content.context;import Android.database.sqlexception;import Android.database.sqlite.sqlitedatabase;import Android.database.sqlite.sqliteopenhelper;import android.database.* Import Android.util.log;public class Notesdbadapter {private static final String TAG = "Notes";p rivate static final Strin G database_name = "Notes.db";p rivate static final int database_version = 1;p rivate static final String database_table = " Notes ";p rivate static final String database_create =" CREATE Table notes ("+" _id INTEGER PRIMARY KEY, "+" note TEXT not NULL, "+" created INTEGER "+"); "; private static class Databasehelper extends Sqliteopenhelper{public databasehelper (context context) {Super (context, database_name,null,database_version);//TODO auto-generated constructor stub} @Override//Create data table public void OnCreate ( Sqlitedatabase db) {//TODO auto-generated MethoD stubtry {db.execsql (database_create); LOG.D (TAG, "OnCreate!");} catch (Exception e) {log.d (TAG, e.tostring ());}} @Override//Update datasheet public void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {try {//TODO auto-generated Meth Od stubdb.execsql ("DROP TABLE IF EXISTS" +database_table); onCreate (db); LOG.D (TAG, "inupgrade!");} catch (Exception e) {log.d (TAG, "Onupgrade failure!");}}} Private Context mctx = null; Abstract interface Private Databasehelper dbhelper; Database Tools class private Sqlitedatabase db; Database class/** Constructor**/public notesdbadapter (Context ctx) {this.mctx=ctx;} Public Notesdbadapter Open () throws Sqlexception{dbhelper = new Databasehelper (mctx);d B = Dbhelper.getwritabledatabase ( );//The database does not exist to create a, if there is a repository to decide whether to update the database return this;} public void Close () {dbhelper.close ();} public static final String ROWID = "_id";p ublic static final String note = "Note";p ublic static final String CREATED = "Created"; string[] Strcols = new string[]{rowid,note,created};//retrieved. The pointer is returned. Less memory public Cursor GetAll () {RETUrn Db.query (Database_table,//which TABLE to Selectstrcols,//which columns to Returnnull,//where Clausenull,//Where Argumentsnull,//group by Clausenull,//having clausenull//order-by clause);} Add public long Create (String Note) {Date now = new Date (); Contentvalues args = new Contentvalues (); Args.put (note, note); Args.put (CREATED, Now.getdate ()); return Db.insert ( Database_table, null, args);}}

SQLite instance--dummynote.java

Related Article

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.