Android platform application development example: Notepad (1)

Source: Internet
Author: User
Tags sqlite database

In this tutorial, a simple list interface will be created, allowing users to add and delete, but not editing. Includes the following content:

◆ Basic knowledge of ListActivities and how to create menu items.

◆ How to use the SQLite database to access data.

◆ How to use ArrayAdapter to bind data to ListView (the simplest way ).

◆ Basic Layout, including how to display a ListView, how to insert a menu item to a menu, and how to respond to the menu

Step 1 create a project

Use Eclipse to create a project Notepad

Step 2: How to Use the Sqlite Database

The Context class is required for system-related interface calls.

1. Open the database

Code snippet:

Public DBHelper (Context ctx ){
Try {
// Open an existing database
Db = ctx. openDatabase (DATABASE_NAME, null );
} Catch (FileNotFoundException e ){
Try {
// Create a new database
Db =
Ctx. createDatabase (DATABASE_NAME, DATABASE_VERSION, 0, null );
// Create a data table
Db.exe cSQL (DATABASE_CREATE );
} Catch (FileNotFoundException e1 ){
Db = null;
}
}
}

OpenDatabase of the Context class can open an existing database. If the database does not exist, a FileNotFoundException exception will be thrown. You can use the createDatabase function of the Context class to create a new database. Call the execSQL method of SQLiteDatabase to execute an SQL statement to create a new data table.

2. Get the data in the table

The Code is as follows:

Public List <Row> fetchAllRows (){
ArrayList <Row> ret = new ArrayList <Row> ();
Try {
Cursor c =
Db. query (DATABASE_TABLE, new String [] {
"Rowid", "title", "body"}, null );
Int numRows = c. count ();
C. first ();
For (int I = 0; I <numRows; ++ I ){
Row row = new Row ();
Row. rowId = c. getLong (0 );
Row. title = c. getString (1 );
Row. body = c. getString (2 );
Ret. add (row );
C. next ();
}
} Catch (SQLException e ){
Log. e ("booga", e. toString ());
}
Return ret;
}

Create a Cursor class Cursor to query a table through the query method of SQLiteDatabase. With Cursor, You can traverse all records.

3. Add a new record

Public void createRow (String title, String body ){
ContentValues initialValues = new ContentValues ();
InitialValues. put ("title", title );
InitialValues. put ("body", body );
Db. insert (DATABASE_TABLE, null, initialValues );
}

 

Construct a ContentValues class. You can call the put method to set the attributes of a record. Add a new record by calling the insert method of SQLiteDatabase.

4. delete records

Public void deleteRow (String str)
{
Db. delete (DATABASE_TABLE, "title = \ '" + str + "\'", null );
}

Directly call the delete method of SQLiteDatabase. The second parameter is an SQL conditional expression.

Step 3: Display Mode and Interface

Most interfaces have a Layout. Our interface is the List displayed in the whole screen. There are other display methods, such as floating window Dialog, Alert ).
Open the main. xml file.
This is a resource file that defines the display style of the interface.

A. All interface resource files start with a line like this: <? Xml version = "1.0" encoding = "UTF-8 "? >.

B. The general type definition is LinearLayout and not always)

C. The definition of XML Namespaces is always the following line:

Xmlns: android = "http://schemas.android.com/apk/res/android"


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.