Advanced Android: CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 19 - 10/30/2012
Advanced Android: CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 19 - 10/30/2012
Passing Information
! In our examples so far ! weve seen one activity launch another activity ! but each activity has been independent of the other ! Were going to look at two additional concepts ! Fragments: reusable bits of UI and behavior that live inside activities ! Passing Information: how do we pass information between activities ! Well also take a look at how an activity can store data into a le that persists between sessions of using the application
Prole Viewer
! Prole viewer will ! Use one activity/fragment to display a list of user names ! This activity can also delete existing users ! Use a second activity/fragment to add new users and edit existing users ! Our program will use Java serialization to persist user names and proles ! The data structure will be a Map<String, ProleData> ! Well discuss ProleData in a moment ! But rst, fragments!
ProleData (I)
! For our Prole Viewer application, our ProleData class stores a users rst name, last name, and e-mail address ! ProleData is implemented as a data holder with getter and setter methods for each attribute ! It implements java.io.Serializable as needed ! It also contains a serialVersionUID attributegenerated by Eclipsethat is used to add support for versioning. ! If we ever change the ProleData class, well need to update the UID. ! Advanced implementations would then use the UID to determine which version a le used and load it using the correct code
10
11
12
13
14
15
17
18
19
20
21
! Simple!
22
Other Highlights
! Prole Viewer also shows ! how to use fragments and how they interact with activities ! how to add menu items to the ActionBar ! how to enable/disable menu items based on list selections ! how to save/load data in onResume() and onPause() to ensure that data is synced between activities
Demo
23
24
25
OnDraw (I)
! Our SimplePaint program allows rectangles to be drawn in four di"erent colors ! We have a data structure that keeps track of the rectangles that have been created and the Paint object used to draw each one ! If we are in the middle of handling a touch event, a rectangle called motionRect exists and we will draw it as well ! Our onDraw method is shown on the next slide
27
OnDraw (II)
protected void onDraw(Canvas canvas) { } } } if (motionRect != null && motionRect.bottom > 0 && motionRect.right > 0) { canvas.drawRect(motionRect, current); canvas.drawRect(0, 0, getWidth(), getHeight(), background); for (Rectangle r : rects) { canvas.drawRect(r.r, r.paint);
28
29
30
31
Other Highlights
! Simple Paint also demonstrates the use of ! a radio group to keep track of the current paint color ! Androids preference mechanism to let the current paint color persist between runs of the application ! You call getSharedPreferences to gain access to a map that contains your apps preferences ! You can read and write preference values in a straightforward manner
Demo
Kenneth M. Anderson, 2012 32
33
SQL Example
! For this example, I recreated Prole Viewer and ! dropped our custom Proles / ProleData classes that made use of Java serialization ! and incorporated the use of an SQLite database ! As you will see, all of the original functionality could be recreated and the resulting program is just a tad simpler ! IF you are comfortable with database programming and SQL; if not, it will seem confusing! ! Note: this version of the program does not use Fragments ! To keep things simple, this program only uses activities to handle the UI
34
SQLiteOpenHelper
! To create a database, you make a subclass of SQLiteOpenHelper ! It takes care of creating and opening a SQLite database for you at run-time ! All you need to do is to supply the CREATE TABLE statement needed to create the table youll be using ! I created a table whose columns correspond to Prole Viewers prole name, rst name, last name, and e-mail address attributes
35
36
CRUD Support
! In databases, you can create, read, update or delete rows in a table ! In Androids database object these correspond to ! insert, query, update, delete ! These are methods, you supply snippets of SQL to these methods; they create the full SQL statement in the background and then execute it against the database
37
38
! The prole = ? is part of an SQL WHERE clause; ! the ? mark is a placeholder ! It gets replaced by the value of the variable name which is passed in via a String array: new String[] { name } is a string array literal in Java
39
40
Wrapping Up
! Learned more about the Android framework ! Passing Information between Activities ! Reading and Writing Files ! 2D Graphics and Touch Events ! Application Preferences ! Working with a Database ! This ends our woefully incomplete review of the Android Framework; however, our three lectures should be enough to get you started!
41
Coming Up Next
! Lecture 20: Advanced iOS ! Homework 4 Due Next Week
42