[Android] Finished lab 4 & 5
Just finished lab4 & 5 together for Android. The lab 4 is creating a general chatting interface with ArrayList which will disappear chatting list(data) after restart the application. The lab 5 is all about SQLite will store and retrieve chatting data.
I won't recap for the lab 4 which isn't that confusing. Mainly, there are three parts for the lab 5.
(1) Creating a database file
(2) Reading from database file
(3) Writing to a database file
Creating a database file
Before using SQLite properly, you gotta create a database file. For the first step, you will need ChatDatabaseHelper class with super. And also, you will need to use onCreate(SQLiteDatabase db) and onUpgrade(SQLiteDatabase db) (+ onDowngrade()).
Reading from database file
If you're familiar with database syntax, it will be much easier to get used to it. For the lab, we're asked modify the class has chatting interface that I created for the lab 4. I will call an object of ChatDatabaseHelper that allows to get writable database. After opening database, retrieving all data from the database by Cursor with RawQuery() or query()(either way is okay), and add ArrayList to update previous chatting list.
Writing to a database file
Using ContentValues object, you will be able to put your data to the ArrayList.
final SQLiteDatabase db = chatDatabase.getWritableDatabase();
.
.
ContentValues contentValues = new ContentValues();
contentValues.put(ChatDatabaseHelper.KEY_MESSAGE, editTextChat.getText().toString());
db.insert(ChatDatabaseHelper.TABLE_NAME, "", contentValues);
Comments
Post a Comment