DatabaseActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
package com.DataManagement.DataBase; import android.app.ListActivity; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.SimpleCursorAdapter; public class DatabaseActivity extends ListActivity { private SQLiteDatabase mDB = null; private DatabaseOpenHelper mDbHelper; private SimpleCursorAdapter mAdapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Create a new DatabaseHelper mDbHelper = new DatabaseOpenHelper(this); // Get the underlying database for writing mDB = mDbHelper.getWritableDatabase(); // start with an empty database clearAll(); // Insert records insertArtists(); // Create a cursor Cursor c = readArtists(); mAdapter = new SimpleCursorAdapter(this, R.layout.list_layout, c, DatabaseOpenHelper.columns, new int[] { R.id._id, R.id.name }, 0); setListAdapter(mAdapter); Button fixButton = (Button) findViewById(R.id.fix_button); fixButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // execute database operations fix(); // Redisplay data mAdapter.getCursor().requery(); mAdapter.notifyDataSetChanged(); } }); } // Insert several artist records private void insertArtists() { ContentValues values = new ContentValues(); values.put(DatabaseOpenHelper.ARTIST_NAME, "Frank Sinatra"); mDB.insert(DatabaseOpenHelper.TABLE_NAME, null, values); values.clear(); values.put(DatabaseOpenHelper.ARTIST_NAME, "Lady Gaga"); mDB.insert(DatabaseOpenHelper.TABLE_NAME, null, values); values.clear(); values.put(DatabaseOpenHelper.ARTIST_NAME, "Jawny Cash"); mDB.insert(DatabaseOpenHelper.TABLE_NAME, null, values); values.clear(); values.put(DatabaseOpenHelper.ARTIST_NAME, "Ludwig von Beethoven"); mDB.insert(DatabaseOpenHelper.TABLE_NAME, null, values); } // Returns all artist records in the database private Cursor readArtists() { return mDB.query(DatabaseOpenHelper.TABLE_NAME, DatabaseOpenHelper.columns, null, new String[] {}, null, null, null); } // Modify the contents of the database private void fix() { // Sorry Lady Gaga :-( mDB.delete(DatabaseOpenHelper.TABLE_NAME, DatabaseOpenHelper.ARTIST_NAME + "=?", new String[] { "Lady Gaga" }); // fix the Man in Black ContentValues values = new ContentValues(); values.put(DatabaseOpenHelper.ARTIST_NAME, "Johnny Cash"); mDB.update(DatabaseOpenHelper.TABLE_NAME, values, DatabaseOpenHelper.ARTIST_NAME + "=?", new String[] { "Jawny Cash" }); } // Delete all records private void clearAll() { mDB.delete(DatabaseOpenHelper.TABLE_NAME, null, null); } // Close database @Override protected void onDestroy() { mDB.close(); mDbHelper.deleteDatabase(); super.onDestroy(); } } |
DatabaseOpenHelper.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
package com.DataManagement.DataBase; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DatabaseOpenHelper extends SQLiteOpenHelper { final static String TABLE_NAME = "artists"; final static String ARTIST_NAME = "name"; final static String _ID = "_id"; final static String[] columns = { _ID, ARTIST_NAME }; final private static String CREATE_CMD = "CREATE TABLE artists (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + ARTIST_NAME + " TEXT NOT NULL)"; final private static String NAME = "artist_db"; final private static Integer VERSION = 1; final private Context mContext; public DatabaseOpenHelper(Context context) { super(context, NAME, null, VERSION); this.mContext = context; } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_CMD); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // N/A } void deleteDatabase() { mContext.deleteDatabase(NAME); } } |
main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@android:id/list"/> <Button android:id="@+id/fix_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/fix_string" android:layout_alignParentBottom="true" android:textSize="24sp"/> </RelativeLayout> |
list_layout.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:baselineAligned="true" android:orientation="horizontal" > <TextView android:id="@+id/_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:textSize="24sp" /> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:textSize="24sp" /> </LinearLayout> |