Another Android SQLite Example

To expose data from Cursor to a ListView widget:

package com.exercise.AndroidSQLite; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteDatabase.CursorFactory; public class SQLiteAdapter { public static final String MYDATABASE_NAME = "MY_DATABASE"; public static final String MYDATABASE_TABLE = "MY_TABLE"; public static final int MYDATABASE_VERSION = 1; public static final String KEY_ID = "_id"; public static final String KEY_CONTENT = "Content"; //create table MY_DATABASE (ID integer primary key, Content text not null); private static final String SCRIPT_CREATE_DATABASE = "create table " + MYDATABASE_TABLE + " (" + KEY_ID + " integer primary key autoincrement, " + KEY_CONTENT + " text not null);"; private SQLiteHelper sqLiteHelper; private SQLiteDatabase sqLiteDatabase; private Context context; public SQLiteAdapter(Context c){ context = c; } public SQLiteAdapter openToRead() throws android.database.SQLException { sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION); sqLiteDatabase = sqLiteHelper.getReadableDatabase(); return this; } public SQLiteAdapter openToWrite() throws android.database.SQLException { sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION); sqLiteDatabase = sqLiteHelper.getWritableDatabase(); return this; } public void close(){ sqLiteHelper.close(); } public long insert(String content){ ContentValues contentValues = new ContentValues(); contentValues.put(KEY_CONTENT, content); return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues); } public int deleteAll(){ return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null); } public Cursor queueAll(){ String[] columns = new String[]{KEY_ID, KEY_CONTENT}; Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns, null, null, null, null, null); return cursor; } public class SQLiteHelper extends SQLiteOpenHelper { public SQLiteHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub db.execSQL(SCRIPT_CREATE_DATABASE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub } } } 

Create /res/layout/row.xml for our ListView

<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dip"/> 

Modify main.xml to change the result(contentlist) from TextView to ListView.

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <ListView android:id="@+id/contentlist" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </LinearLayout> 

Finally, modify AndroidSQLite.java

package com.exercise.AndroidSQLite; import android.app.Activity; import android.database.Cursor; import android.os.Bundle; import android.widget.ListView; import android.widget.SimpleCursorAdapter; public class AndroidSQLite extends Activity { private SQLiteAdapter mySQLiteAdapter; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ListView listContent = (ListView)findViewById(R.id.contentlist); /* * Create/Open a SQLite database * and fill with dummy content * and close it */ mySQLiteAdapter = new SQLiteAdapter(this); mySQLiteAdapter.openToWrite(); mySQLiteAdapter.deleteAll(); mySQLiteAdapter.insert("A for Apply"); mySQLiteAdapter.insert("B for Boy"); mySQLiteAdapter.insert("C for Cat"); mySQLiteAdapter.insert("D for Dog"); mySQLiteAdapter.insert("E for Egg"); mySQLiteAdapter.insert("F for Fish"); mySQLiteAdapter.insert("G for Girl"); mySQLiteAdapter.insert("H for Hand"); mySQLiteAdapter.insert("I for Ice-scream"); mySQLiteAdapter.insert("J for Jet"); mySQLiteAdapter.insert("K for Kite"); mySQLiteAdapter.insert("L for Lamp"); mySQLiteAdapter.insert("M for Man"); mySQLiteAdapter.insert("N for Nose"); mySQLiteAdapter.insert("O for Orange"); mySQLiteAdapter.insert("P for Pen"); mySQLiteAdapter.insert("Q for Queen"); mySQLiteAdapter.insert("R for Rain"); mySQLiteAdapter.insert("S for Sugar"); mySQLiteAdapter.insert("T for Tree"); mySQLiteAdapter.insert("U for Umbrella"); mySQLiteAdapter.insert("V for Van"); mySQLiteAdapter.insert("W for Water"); mySQLiteAdapter.insert("X for X'mas"); mySQLiteAdapter.insert("Y for Yellow"); mySQLiteAdapter.insert("Z for Zoo"); mySQLiteAdapter.close(); /* * Open the same SQLite database * and read all it's content. */ mySQLiteAdapter = new SQLiteAdapter(this); mySQLiteAdapter.openToRead(); Cursor cursor = mySQLiteAdapter.queueAll(); startManagingCursor(cursor); String[] from = new String[]{SQLiteAdapter.KEY_CONTENT}; int[] to = new int[]{R.id.text}; SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to); listContent.setAdapter(cursorAdapter); mySQLiteAdapter.close(); } } 

Android SQLite Example

AndroidSQLite.java

package com.exercise.AndroidSQLite; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class AndroidSQLite extends Activity { private SQLiteAdapter mySQLiteAdapter; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView listContent = (TextView)findViewById(R.id.contentlist); /* * Create/Open a SQLite database * and fill with dummy content * and close it */ mySQLiteAdapter = new SQLiteAdapter(this); mySQLiteAdapter.openToWrite(); mySQLiteAdapter.deleteAll(); mySQLiteAdapter.insert("ABCDE"); mySQLiteAdapter.insert("FGHIJK"); mySQLiteAdapter.insert("1234567"); mySQLiteAdapter.insert("890"); mySQLiteAdapter.insert("Testing"); mySQLiteAdapter.close(); /* * Open the same SQLite database * and read all it's content. */ mySQLiteAdapter = new SQLiteAdapter(this); mySQLiteAdapter.openToRead(); String contentRead = mySQLiteAdapter.queueAll(); mySQLiteAdapter.close(); listContent.setText(contentRead); } } 

SQLiteAdapter.java

package com.exercise.AndroidSQLite; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteDatabase.CursorFactory; public class SQLiteAdapter { public static final String MYDATABASE_NAME = "MY_DATABASE"; public static final String MYDATABASE_TABLE = "MY_TABLE"; public static final int MYDATABASE_VERSION = 1; public static final String KEY_CONTENT = "Content"; //create table MY_DATABASE (ID integer primary key, Content text not null); private static final String SCRIPT_CREATE_DATABASE = "create table " + MYDATABASE_TABLE + " (" + KEY_CONTENT + " text not null);"; private SQLiteHelper sqLiteHelper; private SQLiteDatabase sqLiteDatabase; private Context context; public SQLiteAdapter(Context c){ context = c; } public SQLiteAdapter openToRead() throws android.database.SQLException { sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION); sqLiteDatabase = sqLiteHelper.getReadableDatabase(); return this; } public SQLiteAdapter openToWrite() throws android.database.SQLException { sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION); sqLiteDatabase = sqLiteHelper.getWritableDatabase(); return this; } public void close(){ sqLiteHelper.close(); } public long insert(String content){ ContentValues contentValues = new ContentValues(); contentValues.put(KEY_CONTENT, content); return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues); } public int deleteAll(){ return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null); } public String queueAll(){ String[] columns = new String[]{KEY_CONTENT}; Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns, null, null, null, null, null); String result = ""; int index_CONTENT = cursor.getColumnIndex(KEY_CONTENT); for(cursor.moveToFirst(); !(cursor.isAfterLast()); cursor.moveToNext()){ result = result + cursor.getString(index_CONTENT) + "\n"; } return result; } public class SQLiteHelper extends SQLiteOpenHelper { public SQLiteHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub db.execSQL(SCRIPT_CREATE_DATABASE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub } } } 

main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <TextView android:id="@+id/contentlist" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout>

Get Started with Android Development

Environment Setup

1. Download Eclipse IDE (classic);

2. Install ADT plugin for Eclipse from Help > Install New Software…. and add from https://dl-ssl.google.com/android/eclipse/

3. Download and install the Android SDK;

4. create Android Virtual Device on Eclipse from Windows → AVD Manager

Development

5. From Eclipse, select File → New → Other → Android → Android Project, make sure to choose right target version, input the right Activity name and create the Android project. As a result, res->layout->main.xlm (UI), res->values->strings (attributes), src->yourpackage->youractivity.java (your program), gen->yourpackage->R.java (the linkage), and yourSDK->android.jar (your API) are created.

6. Select the file “res/values/string.xml” and press “Add” more color and strings;

7. Select “res/layout/main.xml” and open the Android editor via a double-click to add more UI elements, and edit theirs properties;

8. Code your application on youractivity.java, including all the event handlers;

Testing

9. Run-As-> Android Application and choose the AVD to run, and wait for the result

(press home and restart your app if necessary on AVD)

Deployment to your device

10. There are three ways to deploy the app directly to your device without going through Android Market.

Protect Your Data on Smart Phones and Tablets

  • (abstracted from http://www.marketwatch.com/story/dont-lose-your-phone-and-your-identity-2011-12-02)
  • Treat your phone with the same amount of security care that you do your computer. Use applications that can help protect you from viruses and malware.
  • Having said that, be careful of what you’re downloading to your phone, Monahan warned. In March, Android Marketplace took more than 50 apps off when they were found to be infected with malware. “Android is the No. 1 operating system for mobile devices because it is an open platform, but it’s also open to criminals,” she said. “Wait for apps to be reviewed, and check out the publisher of the app to make sure it is legitimate. Be careful when giving ‘permissions’ to apps. Does that game you are downloading really need access to your location and your contacts? I think not.”
  • Get password protection and chose a different pass code for your phone than what you use on your computer.
  • Encrypt valuable data, Credant suggested.. Even if it’s lost, many finders will be snooping around.
  • Keep records of what information you have stored on your phone. Some of it, like your bank accounts and passwords, will be impenetrable, according to Gordon, but thieves like to layer bits and pieces of information to get a clearer picture of you.
  • Get remote-wipe capability if it’s offered.
  • Get a find-my-phone function whenever possible. That uses GPS to track down your phone.
  • Check to see if your smartphone can be set so that data is erased automatically after a prescribed number of pass codes have failed. That won’t save the information but could spare your sanity.
  • When shopping this season, put that phone in a safe place and keep it there. Don’t leave it on the tray at food court, on a bench in the dressing room or in the stall in the restroom.