Click here to Skip to main content
15,888,610 members
Articles / Mobile Apps / Android
Tip/Trick

Android - To Do\Shopping List [Tip/Trick] - Part 2

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
13 Jun 2013CPOL2 min read 14.2K   8   2
This tip explains how to create a to do or Shopping List app with listview, sqlite, dialog and customadapter.

Introduction

This tip shows how a Shopping\To do list application is created. It gives an idea of the following:

  1. Adding a list Item in Application
  2. Opening Dialog from Android Activity
  3. Working with SQlite
  4. How to return data from one activity to another

In this tip, we would be discussing about the third point. The first two points are discussed here.

Image 1

Using the Code

Working with SQLite

SQLite

A software library that implements a SQL database engine. It is fast and lightweight, hence best for Mobile devices. The database is stored within a single file. Every Android application can have its own private database. It is being used by many well-known groups/brands like Adobe, Airbus, Apple, Dropbox, General Electric, Google, Intuit, McAfee, Microsoft, php, python, skype, Toshiba, etc.

Database Fundamentals

Database is a collection of structured data organized in the form of tables. Table contains columns to define the attributes of data and rows contain the data records. We can inspect and manipulate database data especially in 4 ways:

  • Add – using INSERT command
  • Delete - using DELETE command
  • Modify - using UPDATE command
  • View – using SELECT command

Using SQLite in Android

Android provides us with a few classes, which can be used while communicating with SQLite:

  1. SQLiteOpenHelper
  2. SQLitedatabase
  3. Cursor

Steps

  1. Derive a helper class from SQLiteOpenHelper.
  2. Override OnCreate method and write query for creating the database and its schema.
  3. In the helper class, write methods for the following:
    • Inserting new records
    • Deleting records
    • Updating records
    • Reading Records

It's done !!!

Here, in this example, we are using one table (NoteTable) with 4 columns (Note id, date/time, name and content).

Here is the sample query for onCreate method for creating database schema.

Java
 String sCreateSQL = "CREATE TABLE IF NOT EXISTS " + 
CommonClass.TABLE_NAME + "( " + CommonClass.COL_SRNO
                + " INTEGER PRIMARY KEY, " + 
                CommonClass.COL_DATETIME + " TEXT, "
                + CommonClass.COL_NOTENAME + " TEXT, " + 
                CommonClass.COL_NOTECONTENT + " TEXT )";
        db.execSQL(sCreateSQL);

db is an object of type SQLiteDatabase, which is passed as a parameter in <code>onCreate function.

For performing any of the operations on data (like insert, update, delete), we need to get an object of SQLiteDatabase. In the derived helper class, by using this object, we can call either getWritableDatabase or getReadabledatabase (depending on our requirement) for getting the database object.

Insert\Update\Delete Records

  1. Get the database object using getWritableDatabase.
  2. Use ContentValues for adding data corresponding to the columns.
  3. Execute using insert\update\delete function.
  4. Don't forget to close the database.
Sample Code
Java
SQLiteDatabase db = this.getWritableDatabase();  //get database Object

//Use contentValues for Insert and update
Java
ContentValues contentvalue = new ContentValues();
contentvalue.put(CommonClass.COL_DATETIME, objShoWData.get_DateTime());
contentvalue.put(CommonClass.COL_NOTECONTENT, objShoWData.get_NoteData());
contentvalue.put(CommonClass.COL_NOTENAME, objShoWData.get_NoteName());
Java
db.insert(CommonClass.TABLE_NAME, null, contentvalue); 

OR

Java
db.update(CommonClass.TABLE_NAME, 
contentvalue, CommonClass.COL_SRNO + " = ?",
new String[] { String.valueOf(objShowdata.get_NoteId()) });

OR

Java
db.delete(CommonClass.TABLE_NAME, CommonClass.COL_SRNO + " = ?",
new String[] { String.valueOf(nId) });
Java
db.close(); //Close database

View Existing Records

  1. Get the database object using getReadableDatabase.
  2. Write the SELECT statement.
  3. Use rawQuery function to execute the SELECT Query.
  4. Iterate the cursor object returned by the rawQuery function.
  5. Retrieve values using getString and getInt, etc.
  6. Don't forget to close the database.

It's done!!!

Java
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);

if (cursor.moveToFirst()) {
do {
    ShowData objShowData = new ShowData();
    objShowData.set_NoteId(cursor.getInt(0));
    objShowData.set_DateTime(cursor.getString(1));
...  ... //Read values from cursor....
    listShowData.add(objShowData);
    } while (cursor.moveToNext());
}
db.close();

History

  • 12th June 2013: Initial release

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Fiserv Inc.
New Zealand New Zealand
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionSource code Pin
Member 42211744-Apr-14 1:13
Member 42211744-Apr-14 1:13 
GeneralMy vote of 5 Pin
henry sentio kauta19-Jun-13 4:21
henry sentio kauta19-Jun-13 4:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.