Friday, May 23, 2014

A simple example using Android's SQLite database, exposes data from Cursor to a ListView

In previous exercise "A simple example using Android's SQLite database", the result of queue was presented as string. It's going to be modified to exposes data from Cursor to a ListView widget.




Modify SQLiteAdapter.java to include a column of "_id" in our database, also include it in the queue. Modify the return of queueAll() to Cursor. Refer to the article "column '_id' does not exist".

  1. package com.exercise.AndroidSQLite;
  2.  
  3. import android.content.ContentValues;
  4. import android.content.Context;
  5. import android.database.Cursor;
  6. import android.database.sqlite.SQLiteDatabase;
  7. import android.database.sqlite.SQLiteOpenHelper;
  8. import android.database.sqlite.SQLiteDatabase.CursorFactory;
  9.  
  10. public class SQLiteAdapter {
  11.  
  12. public static final String MYDATABASE_NAME = "MY_DATABASE";
  13. public static final String MYDATABASE_TABLE = "MY_TABLE";
  14. public static final int MYDATABASE_VERSION = 1;
  15. public static final String KEY_ID = "_id";
  16. public static final String KEY_CONTENT = "Content";
  17.  
  18. //create table MY_DATABASE (ID integer primary key, Content text not null);
  19. private static final String SCRIPT_CREATE_DATABASE =
  20. "create table " + MYDATABASE_TABLE + " ("
  21. + KEY_ID + " integer primary key autoincrement, "
  22. + KEY_CONTENT + " text not null);";
  23.  
  24. private SQLiteHelper sqLiteHelper;
  25. private SQLiteDatabase sqLiteDatabase;
  26.  
  27. private Context context;
  28.  
  29. public SQLiteAdapter(Context c){
  30. context = c;
  31. }
  32.  
  33. public SQLiteAdapter openToRead() throws android.database.SQLException {
  34. sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
  35. sqLiteDatabase = sqLiteHelper.getReadableDatabase();
  36. return this;
  37. }
  38.  
  39. public SQLiteAdapter openToWrite() throws android.database.SQLException {
  40. sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
  41. sqLiteDatabase = sqLiteHelper.getWritableDatabase();
  42. return this;
  43. }
  44.  
  45. public void close(){
  46. sqLiteHelper.close();
  47. }
  48.  
  49. public long insert(String content){
  50.  
  51. ContentValues contentValues = new ContentValues();
  52. contentValues.put(KEY_CONTENT, content);
  53. return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
  54. }
  55.  
  56. public int deleteAll(){
  57. return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);
  58. }
  59.  
  60. public Cursor queueAll(){
  61. String[] columns = new String[]{KEY_ID, KEY_CONTENT};
  62. Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns,
  63. null, null, null, null, null);
  64.  
  65. return cursor;
  66. }
  67.  
  68. public class SQLiteHelper extends SQLiteOpenHelper {
  69.  
  70. public SQLiteHelper(Context context, String name,
  71. CursorFactory factory, int version) {
  72. super(context, name, factory, version);
  73. }
  74.  
  75. @Override
  76. public void onCreate(SQLiteDatabase db) {
  77. // TODO Auto-generated method stub
  78. db.execSQL(SCRIPT_CREATE_DATABASE);
  79. }
  80.  
  81. @Override
  82. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  83. // TODO Auto-generated method stub
  84.  
  85. }
  86.  
  87. }
  88.  
  89. }
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
  1. package com.exercise.AndroidSQLite;
  2.  
  3. import android.app.Activity;
  4. import android.database.Cursor;
  5. import android.os.Bundle;
  6. import android.widget.ListView;
  7. import android.widget.SimpleCursorAdapter;
  8.  
  9. public class AndroidSQLite extends Activity {
  10.  
  11. private SQLiteAdapter mySQLiteAdapter;
  12.  
  13. /** Called when the activity is first created. */
  14. @Override
  15. public void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.main);
  18. ListView listContent = (ListView)findViewById(R.id.contentlist);
  19. /*
  20. * Create/Open a SQLite database
  21. * and fill with dummy content
  22. * and close it
  23. */
  24. mySQLiteAdapter = new SQLiteAdapter(this);
  25. mySQLiteAdapter.openToWrite();
  26. mySQLiteAdapter.deleteAll();
  27.  
  28. mySQLiteAdapter.insert("A for Apply");
  29. mySQLiteAdapter.insert("B for Boy");
  30. mySQLiteAdapter.insert("C for Cat");
  31. mySQLiteAdapter.insert("D for Dog");
  32. mySQLiteAdapter.insert("E for Egg");
  33. mySQLiteAdapter.insert("F for Fish");
  34. mySQLiteAdapter.insert("G for Girl");
  35. mySQLiteAdapter.insert("H for Hand");
  36. mySQLiteAdapter.insert("I for Ice-scream");
  37. mySQLiteAdapter.insert("J for Jet");
  38. mySQLiteAdapter.insert("K for Kite");
  39. mySQLiteAdapter.insert("L for Lamp");
  40. mySQLiteAdapter.insert("M for Man");
  41. mySQLiteAdapter.insert("N for Nose");
  42. mySQLiteAdapter.insert("O for Orange");
  43. mySQLiteAdapter.insert("P for Pen");
  44. mySQLiteAdapter.insert("Q for Queen");
  45. mySQLiteAdapter.insert("R for Rain");
  46. mySQLiteAdapter.insert("S for Sugar");
  47. mySQLiteAdapter.insert("T for Tree");
  48. mySQLiteAdapter.insert("U for Umbrella");
  49. mySQLiteAdapter.insert("V for Van");
  50. mySQLiteAdapter.insert("W for Water");
  51. mySQLiteAdapter.insert("X for X'mas");
  52. mySQLiteAdapter.insert("Y for Yellow");
  53. mySQLiteAdapter.insert("Z for Zoo");
  54. mySQLiteAdapter.close();
  55.  
  56. /*
  57. * Open the same SQLite database
  58. * and read all it's content.
  59. */
  60. mySQLiteAdapter = new SQLiteAdapter(this);
  61. mySQLiteAdapter.openToRead();
  62.  
  63. Cursor cursor = mySQLiteAdapter.queueAll();
  64. startManagingCursor(cursor);
  65.  
  66. String[] from = new String[]{SQLiteAdapter.KEY_CONTENT};
  67. int[] to = new int[]{R.id.text};
  68.  
  69. SimpleCursorAdapter cursorAdapter =
  70. new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
  71.  
  72. listContent.setAdapter(cursorAdapter);
  73. mySQLiteAdapter.close();
  74.  
  75. }
  76. }

No comments:

Post a Comment