Simple notepad implementation and Analysis (2) Compilation of auxiliary classes

Source: Internet
Author: User

1. Create a project

 

Create three java files in the new project: noteedit. Java, notepadv3.java, and notesdbadapter. java.

Noteedit. Java is used as the activity for editing and modifying records.

Notepadv3.java is the activity of the main interface

Notesdbadapter. Java is the class used to operate databases.

Import the resource image to the res/drawablefolder. Only two images are used, both in .9.png format:

 

Ii. Writing notesdbadapter

 

First, write the classes that are frequently used to operate databases,

1.Define the record title, content, and primary key as a String constant.

Public Static FinalString key_title = "title ";
Public Static FinalString key_body = "body ";
Public Static FinalString key_rowid = "_ id ";

 

2.In addition, we must have the following classes: sqliteopenhelper, and sqlitedatabase.

 
PrivateDatabasehelper mdbhelper;
PrivateSqlitedatabase MDB;

 

3.Implement databasehelper to inherit sqliteopenhelper, override the oncreate () and onupgrade () methods, create in oncreate () to database, and onupgrade () to upgrade the database.

 
Public VoidOncreate (sqlitedatabase dB)
{

Db.exe csql (database_create );
}

@ Override
Public VoidOnupgrade (sqlitedatabase dB,IntOldversion,IntNewversion)
{
Log. W (TAG, "upgrading database from version" + oldversion + "to" + newversion
+ ", Which will destroy all old data ");
Db.exe csql ("Drop table if exists notes ");
Oncreate (db );
}

4.New methods for enabling and disabling Databases

 
PublicNotesdbadapter open ()ThrowsSqlexception {}

Public VoidClose (){}

5.Create a new notebook and delete it

 
Public LongCreatenote (String title, string body ){}

Public BooleanDeletenote (LongRowid ){}

6.Query the database and return all records

 
PublicCursor fetchallnotes (){}

7.Query specific records by ID,

 
PublicCursor fetchnote (LongRowid)ThrowsSqlexception {}

8.You can use this method to modify an existing notebook.

 
Public BooleanUpdatenote (LongRowid, String title, string body ){}

For the implementation of the above functions, see the source code.

 

3. Write noteedit

 

1.Obtain the reference of the notesdbadapter and declare the textview of the title and content.

PrivateNotesdbadapter mdbhelper;
PrivateEdittext mtitletext;
PrivateEdittext mbodytext;
PrivateLong mrowid;

2. Override oncreate (), onresume (), onpause (), onsaveinstancestate () method:

Oncreate () is mainly used to determine the value of the input parameter savedinstancestate, and assign the corresponding values of textviews on the page to the populatefields () method, add a listener for the OK button.

implement the populatefields () method called in oncreate (), query the database through the obtained id value, and display the data to the page

  If  (mrowid! =  null ) 
{< br> cursor note = mdbhelper. fetchnote (mrowid);
startmanagingcursor (Note);
mtitletext
. settext (note. getstring (note. getcolumnindexorthrow (notesdbadapter. key_title);
mbodytext. settext (note. getstring (note. getcolumnindexorthrow (notesdbadapter. key_body);
}

Another key point is to save the positive notes to the database. Implemented in onsave ()

Private VoidSavestate ()
{
String title = mtitletext. gettext (). tostring ();
String body = mbodytext. gettext (). tostring ();

If(Mrowid =Null)
{
LongId = mdbhelper. createnote (title, body );
If(ID> 0)
{
Mrowid = ID;
}
}Else
{
Mdbhelper. updatenote (mrowid, title, body );
}
}

 

The onsave () implemented above should be called in onsaveinstancestate () and onpause () to avoid data loss.

 

Not complete...

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.