Mobile Application Development Chapter 5 [Persistent Data in Android]

Slides:



Advertisements
Similar presentations
Bruce Scharlau, University of Aberdeen, 2012 Data storage options for mobiles Mobile Computing.
Advertisements

Copyright  Oracle Corporation, All rights reserved. 1 Creating an Application: The AppBuilder for Java IDE.
Chapter 3: Engage! Android User Input, Variables, and Operations
 data/data-storage.html#pref data/data-storage.html#pref 
Guide to Oracle10G1 Introduction To Forms Builder Chapter 5.
XP New Perspectives on Microsoft Office Access 2003, Second Edition- Tutorial 2 1 Microsoft Office Access 2003 Tutorial 2 – Creating And Maintaining A.
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
Creating And Maintaining A Database. 2 Learn the guidelines for designing databases When designing a database, first try to think of all the fields of.
Introduction To Form Builder
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
1 Computing for Todays Lecture 16 Yumei Huo Fall 2006.
Android Development (Basics)
Data Persistence in Android
SQLite Database. SQLite Public domain database – Advantages Small (about 150 KB) – Used on devices with limited resources Each database contained within.
XP New Perspectives on Microsoft Access 2002 Tutorial 41 Microsoft Access 2002 Tutorial 4 – Creating Forms and Reports.
INSERT BOOK COVER 1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Access 2010 by Robert Grauer, Keith.
Favorite Twitter® Searches App Android How to Program © by Pearson Education, Inc. All Rights Reserved.
Tip Calculator App Building an Android App with Java © by Pearson Education, Inc. All Rights Reserved.
Engage! Android User Input, Variables,
XP New Perspectives on Microsoft Access 2002 Tutorial 21 Microsoft Access Tutorial 2 – Creating And Maintaining A Database.
XP 1 Microsoft Access 2003 Introduction To Microsoft Access 2003.
Android Boot Camp for Developers Using Java, 3E
Nilesh Singh Local Data Storage option Android provides several options for you to save persistent application data. - Shared preferences - Creation.
9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.
XP New Perspectives on Microsoft Access 2002 Tutorial 1 1 Microsoft Access 2002 Tutorial 1 – Introduction To Microsoft Access 2002.
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 41 JavaServer Face.
Mobile Programming Lecture 3 Debugging. Lecture 2 Review What widget would you use to allow the user to enter o a yes/no value o a range of values from.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 25.1 Test-Driving the ATM Application 25.2.
Mobile Software Development ISCG 7424 Department of Computing UNITEC John Casey and Richard Rabeder SQLite and Permissions.
Class Builder Tutorial Presented By- Amit Singh & Sylendra Prasad.
INSERT BOOK COVER 1Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Getting Started with VBA for Microsoft Office 2010 by.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.
Android - SQLite Database 12/10/2015. Introduction SQLite is a opensource SQL database that stores data to a text file on a device. Android comes in with.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Address Book Application Introducing Database Programming.
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.
Chapter 5 Introduction To Form Builder. Lesson C Objectives  Use sequences to automatically generate primary key values in a form  Create lists of values.
CHAPTER 9 File Storage Shared Preferences SQLite.
XP New Perspectives on Microsoft Office Access 2003 Tutorial 4 1 Microsoft Office Access 2003 Tutorial 4 – Creating Forms and Reports.
Chapter 10 Using Macros, Controls and Visual Basic for Applications (VBA) with Excel Microsoft Excel 2013.
By: Eliav Menachi.  On Android, all application data (including files) are private to that application  Android provides a standard way for an application.
Copyright © 2014 Pearson Canada Inc. Ext. 5b-1 Copyright © 2014 Pearson Canada Inc. Application Extension 5b Using Microsoft Access Part 2: Using Information.
Data Storage in Android Димитър Н. Димитров. Why talk about data? Why not 3D graphics or network connectivity? Data as fundamental term in computer science.
ASP.NET Programming with C# and SQL Server First Edition
Tutorial 1 Creating a Database
SQLite in Android Landon Cox March 2, 2017.
Practical Office 2007 Chapter 10
Mobile Applications (Android Programming)
Android Application SQLite 1.
Microsoft Office Access 2003
Android Database using SQLite
Mobile Application Development Chapter 4 [Android Navigation and Interface Design] IT448-Fall 2017 IT448- Fall2017.
Access Creating a Database
Sensors, maps and fragments:
Access Creating a Database
Important terms Black-box testing White-box testing Regression testing
Important terms Black-box testing White-box testing Regression testing
Debugging with Eclipse
Exploring Microsoft® Access® 2016 Series Editor Mary Anne Poatsy
MODULE 7 Microsoft Access 2010
Access Lesson 2 Creating a Database
Testing, debugging, and using support libraries
Microsoft Office Access 2003
Guidelines for Microsoft® Office 2013
Mobile Programming Dr. Mohsin Ali Memon.
Debugging with Eclipse
Presentation transcript:

Mobile Application Development Chapter 5 [Persistent Data in Android] IT448-Fall 2017 IT448- Fall2017

Contents Data persistence Preference, files and databases Saving Key-Value sets Saving Files Saving Data in SQL databases

Data persistence The three approaches to data persistence SharedPreferences, Standard flat file input/output, and SQLite database system. Preferences are implemented through use of the SharedPreferences class. There are two main modes for accessing SharedPreferences: getSharedPreferences ("String preference name", integer mode) getPreferences(integer mode). Files are written and read as a stream of bytes. This means that to the Android system, a file is one thing. It does not have parts. Figure: From Learning Mobile App Development by Jakob Iversen and Michael Eierman (ISBN: 032194786X) Copyright © 2014 Pearson Education, Inc. All rights reserved.

Files are written and read as a stream of bytes. Files can be written to either internal or external storage. Android supports the use of SQLite databases. The MyContactList app uses a simple, one table database to provide the data storage and manipulation functionality. The recommended approach to using SQLite in an Android app is to create a Database Helper class whose only function is to provide for the creation, modification, and deletion of tables in the database. Figure: From Learning Mobile App Development by Jakob Iversen and Michael Eierman (ISBN: 032194786X) Copyright © 2014 Pearson Education, Inc. All rights reserved.

A static variable is declared to name the database. Explanation of code: The class ContactDBHelper is declared as a subclass of SQLiteOpenHelper A static variable is declared to name the database. A static variable to hold the database version number is declared and initialized to 1. A string variable is declared and assigned to a SQL command that creates the table. Figure: From Learning Mobile App Development by Jakob Iversen and Michael Eierman (ISBN: 032194786X) Copyright © 2014 Pearson Education, Inc. All rights reserved.

Saving Data in Apps https://developer.android.com/training/basics/data-storage/index.html

Explanation of code (contd.): The constructor method calls the super class’s constructor method. The onCreate method is called the first time the database is opened. The onUpgrade method is executed when the database is opened and the current version number in the code is higher than the version number of the current database. Figure: From Learning Mobile App Development by Jakob Iversen and Michael Eierman (ISBN: 032194786X) Copyright © 2014 Pearson Education, Inc. All rights reserved.

Figure 5.1 Code with breakpoint set. From Learning Mobile App Development by Jakob Iversen and Michael Eierman (ISBN: 032194786X) Copyright © 2014 Pearson Education, Inc. All rights reserved.

Create a new class named ContactDataSource (Listing 5.2) Create the Data Source Class: Create a new class named ContactDataSource (Listing 5.2) Variables are declared to hold instances of the SQLite database and the helper class. The helper class is instantiated when the data source class is instantiated. Open and close methods are used to access and end access to the database. The rest of the code in this class is dependent on the needs of the app. The Contact class does not exist, so the first task is to create that class. Create another new class in the src folder named Contact (Listing 5.3). Figure: From Learning Mobile App Development by Jakob Iversen and Michael Eierman (ISBN: 032194786X) Copyright © 2014 Pearson Education, Inc. All rights reserved.

Create the Data Source Class: The Contact class is a very simple class. After the Contact class has been created, you can now code the insert and update methods in the ContactDataSource class. (Listing 5.4). The two methods are very similar. The primary difference is that the updateContact method uses the Contact ID to overwrite values in the Contact table. The insertContact method just inserts contact data and the database inserts the ID because the _id field was declared as an autoincrement field. Figure: From Learning Mobile App Development by Jakob Iversen and Michael Eierman (ISBN: 032194786X) Copyright © 2014 Pearson Education, Inc. All rights reserved.

Similarity of Two Methods: A Boolean variable is declared and assigned the value false. The ContentValues object is used to store a set of key/value pairs that are used to assign contact data to the correct field in the table. The values for the table are retrieved from the Contact object, associated with the correct field, and inserted into the ContentValues object. Figure: From Learning Mobile App Development by Jakob Iversen and Michael Eierman (ISBN: 032194786X) Copyright © 2014 Pearson Education, Inc. All rights reserved.

Similarity of Two Methods: The database’s insert method is called and passed the name of the table and values to insert. If the method throws an exception, the return value is already set to false, so we don’t have to do anything. The update procedure needs the contact’s ID to correctly update the table. The database’s update method is called to place the changes in the database. Figure: From Learning Mobile App Development by Jakob Iversen and Michael Eierman (ISBN: 032194786X) Copyright © 2014 Pearson Education, Inc. All rights reserved.

Using the Database: The three classes: Contact , ContactDBHelper , and ContactDataSource , are used in the ContactActivity class to implement the saving of contact data to the database. The first step is to provide an association between the ContactActivity class and a Contact object. Figure: From Learning Mobile App Development by Jakob Iversen and Michael Eierman (ISBN: 032194786X) Copyright © 2014 Pearson Education, Inc. All rights reserved.

Using the Database: Next associate the currentContact variable with a new Contact object by entering the following code as the last line in the onCreate method: currentContact = new Contact() ; The final step in modifying existing code is to add a line of code in the didFinishDatePickerDialog method to store the selected birthday in the Contact object. Add the following line of code as the last line of code in that method: currentContact .setBirthday(selectedTime) Figure: From Learning Mobile App Development by Jakob Iversen and Michael Eierman (ISBN: 032194786X) Copyright © 2014 Pearson Education, Inc. All rights reserved.

Capture User-Entered Data: The first new method needed is used to capture the user data as it’s typed and store it in the currentContact object. To start, enter the following line of code after all the other init methods in the onCreate method: initTextChangedEvents ( ) ; Next, create a new method in the ContactActivity class called initTextChangedEvents ( ) . Place this method after the other init methods currently in the class. Enter the code in Listing 5.5 Listing 5.5 is not the complete method. A listener has to be added for all the other EditTexts in the layout. Figure: From Learning Mobile App Development by Jakob Iversen and Michael Eierman (ISBN: 032194786X) Copyright © 2014 Pearson Education, Inc. All rights reserved.

The code is essentially the same for each EditText. A reference to the Contact Name EditText is assigned to the variable contactName. A TextChangedListener is added to the EditText by creating a new TextWatcher object. The afterTextChanged method is a required method for the TextWatch object. This code is executed when the user ends editing of the EditText The beforeTextChanged method is a required TextWatcher method. Figure: From Learning Mobile App Development by Jakob Iversen and Michael Eierman (ISBN: 032194786X) Copyright © 2014 Pearson Education, Inc. All rights reserved.

The code is essentially the same for each EditText. The onTextChanged method is also a required TextWatcher method. The pattern repeats for another EditText in the layout, except that value is assigned to a different attribute of the currentContact object. This code gets the value entered into the editAddress EditText , converts it to a string, and sets the streetAddress attribute of the currentContact object to that value. This code needs to be repeated for the remaining EditText s in the activity_contact.xml file. Next set the phone number EditText s to autoformat the number as it’s typed. Finally, add initTextChangedEvents(); to the onCreate method. Figure: From Learning Mobile App Development by Jakob Iversen and Michael Eierman (ISBN: 032194786X) Copyright © 2014 Pearson Education, Inc. All rights reserved.

Save User-Entered Data: The intTextChangedEvents method sets the stage for saving the contact’s data. It sets the EditText s to update the ContactActivity ’s contact object with any changes users make as they make them. All that is left is to pass that object to the insert or update method in the ContactDataSource class so that the changes can be stored in the database. The initialization of the Save button is similar to the initialization of all the other buttons coded. Figure: From Learning Mobile App Development by Jakob Iversen and Michael Eierman (ISBN: 032194786X) Copyright © 2014 Pearson Education, Inc. All rights reserved.

Enter the code in Listing 5.6 before the initTextChangedEvents method. Call the method in the onCreate method where the rest of the button initialization methods are called. The only new code in this method is the code associated with the save operation. A new ContactDataSource object is instantiated. The database is opened. A Boolean variable is declared and set to false. The currentContact’s id is compared to -1. The database is closed as soon as possible. The return value is checked. Figure: From Learning Mobile App Development by Jakob Iversen and Michael Eierman (ISBN: 032194786X) Copyright © 2014 Pearson Education, Inc. All rights reserved.

Test the code on the emulator. Fix any discrepancies. Repeat the last two lines of code in Listing 5.7 for each EditText in the layout. Test the modification in the emulator. Fix any discrepancies. Open the ContactDataSource and create a new method using the code in Listing 5.8. Figure: From Learning Mobile App Development by Jakob Iversen and Michael Eierman (ISBN: 032194786X) Copyright © 2014 Pearson Education, Inc. All rights reserved.

The cursor is told to move to the first record in the returned data. The structure of this method is similar to the other insert and update methods. An SQL query is written to get the maximum value for the _id field in the contact table. A cursor is declared and assigned to hold the results of the execution of the query. The cursor is told to move to the first record in the returned data. The maximum ID is retrieved from the recordset. The cursor is closed. Figure: From Learning Mobile App Development by Jakob Iversen and Michael Eierman (ISBN: 032194786X) Copyright © 2014 Pearson Education, Inc. All rights reserved.

Use the Debugger: To test if the functionality just added is working properly you have to watch it run. This is done using the debugger. To check whether the currentContact is getting a new ID, set a breakpoint in the initSaveButton method on the hideKeyboard() line ( Figure 5.1 ). To control execution during debugging, use the Debug toolbar buttons at the top of the perspective (See Figure 5.2 ). Figure: From Learning Mobile App Development by Jakob Iversen and Michael Eierman (ISBN: 032194786X) Copyright © 2014 Pearson Education, Inc. All rights reserved.

Use the Debugger (contd): Click the Step Over button and watch the code step line by line until you reach the currentContact.setContactID(newId) line. On the upper right of the perspective is a pane with two tabs: Variables and Breakpoints (see Figure 5.3 ). Click the Variables tab. In the Variables tab, find the newId variable and verify that its value is greater than -1. The debugger can be stopped in several ways. The first would be to click the Terminate button in the toolbar. An alternative to stepping through the code with the debugger is to use logging. Figure: From Learning Mobile App Development by Jakob Iversen and Michael Eierman (ISBN: 032194786X) Copyright © 2014 Pearson Education, Inc. All rights reserved.

Open the activity_contact_settings.xml file. SharedPreferences are an easy way to store bits of information that need to persist over the life cycle of an app. Settings Layout Open the activity_contact_settings.xml file. In Graphical Layout, drag two medium TextViews and two RadioGroup widgets from the Form Widgets folder in the Palette to the layout. Switch to activity_contact_settings .xml and modify the XML to match Listing 5.9 . After the modifications have been made, switch to Graphical Layout and verify that the layout looks like Figure 5.5 . Figure: From Learning Mobile App Development by Jakob Iversen and Michael Eierman (ISBN: 032194786X) Copyright © 2014 Pearson Education, Inc. All rights reserved.

Figure 5.4 ContactSettings layout loading widgets. From Learning Mobile App Development by Jakob Iversen and Michael Eierman (ISBN: 032194786X) Copyright © 2014 Pearson Education, Inc. All rights reserved.

Code the Page’s Behavior Open the ContactSettingsActivity .java file and complete the following steps: Copy the ImageButton initialization methods from the ContactActivity .java to the ContactSettingsActivity .java file so that the navigation bar will work. When the code is pasted into the ContactSettingActivity class, it will produce errors. Change this for each to be the ContactSettingActivity. Figure: From Learning Mobile App Development by Jakob Iversen and Michael Eierman (ISBN: 032194786X) Copyright © 2014 Pearson Education, Inc. All rights reserved.

Call the three methods in the onCreate method. Change the code in the initSettingsButton() method so that the button is disabled. Call the three methods in the onCreate method. Create a method called initSettings and refer to the code in Listing 5.10 to get it to properly configure the activity at startup. Figure: From Learning Mobile App Development by Jakob Iversen and Michael Eierman (ISBN: 032194786X) Copyright © 2014 Pearson Education, Inc. All rights reserved.

The initSettings method gets the values stored in SharedPreferences to set the RadioButtons to the value that the user checks. A string variable is declared, and the value for the field to sort contacts by is retrieved from SharedPreferences. A reference to each radio button in the sort field RadioGroup is assigned to a variable. The value retrieved for the preferred sort field is evaluated to determine which RadioButton should be set as checked. The same operations are performed to set the sort order to the order preferred by the user. Figure: From Learning Mobile App Development by Jakob Iversen and Michael Eierman (ISBN: 032194786X) Copyright © 2014 Pearson Education, Inc. All rights reserved.

The only new code is the code used to save the selected preference. The next step is to create a method to store the selected user preference for each option. Refer to Listing 5.11 for the code for these methods. The only new code is the code used to save the selected preference. Add calls to these methods in the onCreate method and then run and test the Settings activity. Remember, data that exists beyond the execution of the app is said to persist. Figure: From Learning Mobile App Development by Jakob Iversen and Michael Eierman (ISBN: 032194786X) Copyright © 2014 Pearson Education, Inc. All rights reserved.

References Saving Data in Apps https://developer.android.com/training/basics/data-storage/index.html