When I first touched Android, I used eclipse to write small apps, then manually ported to Android Studio, you can create new, view, delete notes, and so on, today to clean up the code, the full code can be seen on my GitHub, link at the end of the text.
DBHelper:
Package Com.zms.notepad;import Android.content.contentvalues;import Android.content.context;import Android.database.cursor;import Android.database.sqlite.sqlitedatabase;import Android.database.sqlite.sqliteopenhelper;import java.util.arraylist;import java.util.list;/** * Created by AlexZhou On 2015/2/3. * 11:12 */public class Notedbhelper extends Sqliteopenhelper {private static final int database_version = 2; private static final String database_name = "notes_db"; private static final String Note_table_name = "NOTE"; private static final String note_col_id = "_id"; private static final String Note_col_title = "TITLE"; private static final String note_col_content = "CONTENT"; private static final String note_col_created = "CREATED"; private static final String note_col_modified = "MODIFIED"; private static final string[] note_col_projection = new string[] {note_col_id, note_col_title, Note_col_content, NOTE_COL_CREated, note_col_modified}; Public Notedbhelper (Context context) {Super (context, database_name, NULL, database_version); }//CREATE TABLE @Override public void onCreate (Sqlitedatabase db) {String Createnotetablesql = "Create TABLE "+ Note_table_name +" ("+ note_col_id +" INTEGER PRIMARY KEY autoincrement, "+ note_col_title + "text," + note_col_content + "text," + note_col_created + "INTEGER," + note_col_modified + "INTEGER" + ");"; Db.execsql (Createnotetablesql); } @Override public void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {//Drop older table if existed Db.execsql ("DROP TABLE IF EXISTS" + note_table_name); Create Tables again onCreate (db); }//ADD new Note public int addnote (Note Note) {Sqlitedatabase db = This.getwritabledatabase (); Contentvalues values = new Contentvalues (); Values.Put (Note_col_title, Note.gettitle ()); Values.put (Note_col_content, Note.getcontent ()); Values.put (note_col_created, note.getcreated ()); Values.put (Note_col_modified, note.getmodified ()); Insert to database long rowId = Db.insert (note_table_name, null, values); Close the database Db.close (); return (int) rowId; }//Get One note public note getnote (int id) {Sqlitedatabase db = This.getreadabledatabase (); cursor cursor = db.query (Note_table_name, note_col_projection, note_col_id + "=?", new string[] {String.va Lueof (ID)}, NULL, NULL, NULL, NULL); if (cursor! = NULL) Cursor.movetofirst (); Note Note = new Note (cursor.getint (0), cursor.getstring (1), cursor.getstring (2), Cursor.getlong (3), cursor. Getlong (4)); return note; }//Get all notes public list<note> getallnotes () {list<note> notelist = new ARRAylist<note> (); String selectquery = "SELECT * from" + note_table_name; Sqlitedatabase db = This.getwritabledatabase (); cursor cursor = db.rawquery (selectquery, NULL); Looping through all rows and adding to list if (Cursor.movetofirst ()) {do {note note = New Note (cursor.getint (0), cursor.getstring (1), cursor.getstring (2), Cursor.getlong (3), Cursor.getlong (4)); Notelist.add (note); } while (Cursor.movetonext ()); }//Return contact list return notelist; Public Cursor Getallnotescursor () {String selectquery = ' select * from ' + note_table_name; Sqlitedatabase db = This.getreadabledatabase (); cursor cursor = db.rawquery (selectquery, NULL); return cursor; } public int Updatenote (Note Note) {Sqlitedatabase db = This.getwritabledatabase (); Contentvalues values = new ContenTvalues (); Values.put (Note_col_title, Note.gettitle ()); Values.put (Note_col_content, Note.getcontent ()); Values.put (note_col_created, note.getcreated ()); Values.put (Note_col_modified, note.getmodified ()); Return Db.update (Note_table_name, values, note_col_id + "=?", new string[] {string.valueof (Note.getid ())} ); } public void Deletenote (int noteid) {Sqlitedatabase db = This.getwritabledatabase (); Db.delete (Note_table_name, note_col_id + "=?", new string[] {string.valueof (Noteid)}); Db.close (); } public void Deleteallnotes () {Sqlitedatabase db = This.getwritabledatabase (); Db.delete (note_table_name, NULL, NULL); Db.close (); }}
To create and modify notes:
Package Com.zms.notepad;import Android.app.activity;import Android.content.intent;import android.os.Bundle;import Android.view.menu;import Android.view.menuitem;import Android.view.view;import Android.widget.Button;import Android.widget.edittext;import android.widget.toast;/** * Created by Alexzhou on 2015/2/3. * 11:14 */public class Notenew extends Activity {private EditText ettitle; Note Title Private EditText etcontent; Note Content private Button btncancel; Private Button btnsave; private int _noteid; Note ID private notedbhelper _db; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.note_new); Ettitle = (EditText) Findviewbyid (r.id.ettitle); Etcontent = (EditText) Findviewbyid (r.id.etcontent); Btncancel = (Button) Findviewbyid (r.id.btncancel); Btnsave = (Button) Findviewbyid (R.id.btnsave); Btncancel.setonclicklistener (New OnclicklIstenerimp ()); Btnsave.setonclicklistener (New Onclicklistenerimp ()); _db = new Notedbhelper (this); Intent Intent = Getintent (); _noteid = Intent.getintextra (notelist.extra_note_id,-1); if (_noteid > 0) {Note Note = _db.getnote (_noteid); Ettitle.settext (Note.gettitle ()); Etcontent.settext (Note.getcontent ()); }} Private class Onclicklistenerimp implements View.onclicklistener {@Override public void OnClick (V Iew v) {if (v = = btncancel) {toast.maketext (Notenew.this, "Apocalypse prompt: Discard new note", Toast.length_short). s how (); Finish (); } else if (v = = btnsave) {String titlevoid = Ettitle.gettext (). toString (); String contentvoid = Etcontent.gettext (). toString (); if (Titlevoid.equals ("") | | Contentvoid.equals (")) {Toast.maketext (" ") {notenew.this," Apocalypse tip: Title or content is empty ", Toast.length_short). Show (); } else {todatabase (); Insert Database Toast.maketext (Notenew.this, "Apocalypse: Note saved successfully", Toast.length_short). Show (); Finish (); }}}} @Override public boolean Oncreateoptionsmenu (Menu menu) {Getmenuinflater (). infl Ate (R.menu.note_new_menu, menu); return true; } @Override public boolean onoptionsitemselected (MenuItem item) {int id = item.getitemid (); if (id = = r.id.action_exit) {Intent Intent = new Intent (intent.action_main); Intent.addcategory (Intent.category_home); Intent.setflags (Intent.flag_activity_clear_top); StartActivity (Intent); Android.os.Process.killProcess (Android.os.Process.myPid ()); return true; } else if (id = = r.id.action_about) {Intent Intent = new Intent (this, about.class); StartActivity (Intent); return true; } RETurn super.onoptionsitemselected (item); Public final int todatabase () {String title = Ettitle.gettext (). toString (); String content = Etcontent.gettext (). toString (); int Newnoteid =-1; if (_noteid > 0) {Note Note = _db.getnote (_noteid); Note.settitle (title); Note.setcontent (content); Note.setmodified (Long.valueof (System.currenttimemillis ())); _db.updatenote (note); } else {Note newnote = new Note (title, content); Newnoteid = _db.addnote (newnote); } return Newnoteid; }}
To view notes:
Package Com.zms.notepad;import Android.content.intent;import Android.support.v7.app.actionbaractivity;import Android.os.bundle;import Android.view.menu;import Android.view.menuitem;import Android.view.View;import Android.widget.button;import Android.widget.imageview;public class Main extends Actionbaractivity {private ImageView I Vlogo; Private Button Btnview; Private Button btnnew; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); Ivlogo = (ImageView) Findviewbyid (R.id.ivlogo); Btnview = (Button) Findviewbyid (R.id.btnview); Btnnew = (Button) Findviewbyid (r.id.btnnew); Btnview.setonclicklistener (New Onclicklistenerimp ()); Btnnew.setonclicklistener (New Onclicklistenerimp ()); Ivlogo.setalpha (50); Alpha 0-255, set the transparency of the Home page logo} Private class Onclicklistenerimp implements View.onclicklistener {@Override public void OnCLick (View v) {if (v = = Btnview) {Intent Intent = new Intent (main.this, Notelist.class); StartActivity (Intent); } else if (v = = btnnew) {Intent Intent = new Intent (main.this, Notenew.class); StartActivity (Intent); }}} @Override public boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (r.menu.ma in, menu); return true; } @Override public boolean onoptionsitemselected (MenuItem item) {int id = item.getitemid (); if (id = = r.id.action_about) {//Click on the Menu "about" button to trigger Intent Intent = new Intent (this, about.class); StartActivity (Intent); return true; } else if (id = = R.id.action_exit) {//Click the menu "Exit" button to trigger Intent Intent = new Intent (intent.action_main) ; Intent.addcategory (Intent.category_home); Intent.setflags (Intent.flag_activity_clear_top); StartActivity (Intent); Android.os.Process.killProcess (Android.os.Process.myPid ()); return true; } return super.onoptionsitemselected (item); }}
Reprint Please specify source: Zhou Mushi's csdn blog Http://blog.csdn.net/zhoumushui
My github: Zhou Mushi's GitHub Https://github.com/zhoumushui
Android Notepad Notepad