+ All Categories
Home > Documents > 2016 Cengage Learning. May not be scanned, copied or duplicated, or posted to a publicly accessible...

2016 Cengage Learning. May not be scanned, copied or duplicated, or posted to a publicly accessible...

Date post: 18-Jan-2018
Category:
Upload: aldous-baker
View: 232 times
Download: 0 times
Share this document with a friend
Description:
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Objectives (continued) Write data using a SharedPreferences object Instantiate a SharePreferences object Write data using getString( ) method Retrieve data from a SharedPreferences object Read data using a putString( ) method Display an ImageView control using code 3 Android Boot Camp for Developers Using Java, 3rd Ed.

If you can't read please download the document

Transcript

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 for Developers Using Java, 3E Chapter 11: Discover! Persistent Data 1 Android Boot Camp for Developers Using Java, 3rd Ed. 2016 Cengage Learning. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Objectives In this chapter, you learn to: Create an Android project using persistent data Understand different types of persistent data Understand SharedPreferences persistent data Understand internal storage Understand external storage Understand saving data using a network connection Understand saving to a database connection 2 Android Boot Camp for Developers Using Java, 3rd Ed. 2016 Cengage Learning. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Objectives (continued) Write data using a SharedPreferences object Instantiate a SharePreferences object Write data using getString( ) method Retrieve data from a SharedPreferences object Read data using a putString( ) method Display an ImageView control using code 3 Android Boot Camp for Developers Using Java, 3rd Ed. 2016 Cengage Learning. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Understanding Persistent Data Steps to complete the App: 1.Add strings to the String table. 2.Add images to the drawable folder. 3.Design two XML layouts for the first and second Activity. 4.Instantiate the XML controls in the first Activity. 5.Establish a SharedPreferences object to store the data entered. 6.Write data to the SharedPreferences object. 7.Launch a second Activity. 8.Initialize the XML controls on the second Activity. 9.Retrieve the data from the SharedPreferences object. 10.Calculate the monthly payment and display the appropriate image for the number of loan years using an If structure. 11.Display the monthly payment on the second Activity. 4 Android Boot Camp for Developers Using Java, 3rd Ed. 2016 Cengage Learning. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Understanding Persistent Data (continued) Persistent data stores values permanently by placing the information in a file Can be stored in five different ways in Android applications 5 Android Boot Camp for Developers Using Java, 3rd Ed. 2016 Cengage Learning. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Understanding Persistent Data (continued) Shared preferences Stores private data in keyvalue pairs Internal storage Stores private data in the memory of the device External storage Stores data, which can be available to other apps on shared external storage SQLite database Stores structured data in a private database Network connection Stores data on a Web server 6 Android Boot Camp for Developers Using Java, 3rd Ed. 2016 Cengage Learning. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Understanding Persistent Data (continued) 7 Android Boot Camp for Developers Using Java, 3rd Ed. Using Shared Preferences Can save any data including user preferences, such as what wallpaper a user has chosen or individual values entered by the user in an EditText control Can be used to save any primitive data: Booleans, floats, ints, longs, and strings 2016 Cengage Learning. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Understanding Persistent Data (continued) 8 Android Boot Camp for Developers Using Java, 3rd Ed. Using Internal Storage Store the information directly on the devices internal drive Saved files on the device are available only to the app that created the files Be careful - low internal storage space can drastically affect the speed of an Android device and battery life 2016 Cengage Learning. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Understanding Persistent Data (continued) 9 Android Boot Camp for Developers Using Java, 3rd Ed. Using External Storage Store the information on the devices SD (Secure Digital) card All applications can read and write files placed on the external storage and the Android smartphone or tablet owner can remove them To use external storage, the following permissions are necessary in the Android Manifest file: 2016 Cengage Learning. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Understanding Persistent Data (continued) 10 Android Boot Camp for Developers Using Java, 3rd Ed. Using SQLite Databases Perfect for a large amount of data Available since the Cupcake 1.5 version of Android and occupies a small amount of disk memory Android apps model data items in tables and columns, with optional relationships between the entities within the database The tables can be queried using SQL statements 2016 Cengage Learning. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Understanding Persistent Data (continued) 11 Android Boot Camp for Developers Using Java, 3rd Ed. Using a Network Connection If connected to the Internet (a 3G/4G or Wi-Fi connection), data can be stored and retrieved on a Web service If a connection is not available, the user cannot save or retrieve the persistent data 2016 Cengage Learning. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Creating XML Layout Files 12 Android Boot Camp for Developers Using Java, 3rd Ed. 2016 Cengage Learning. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Creating XML Layout Files (continued) 13 Android Boot Camp for Developers Using Java, 3rd Ed. 2016 Cengage Learning. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Creating XML Layout Files (continued) 14 Android Boot Camp for Developers Using Java, 3rd Ed. 2016 Cengage Learning. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Creating a Second Activity and XML Layout 15 Android Boot Camp for Developers Using Java, 3rd Ed. 2016 Cengage Learning. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Creating a Second Activity and XML Layout (continued) 16 Android Boot Camp for Developers Using Java, 3rd Ed. 2016 Cengage Learning. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Instantiating the XML Controls 17 Android Boot Camp for Developers Using Java, 3rd Ed. 2016 Cengage Learning. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Instantiating the XML Controls (continued) 18 Android Boot Camp for Developers Using Java, 3rd Ed. 2016 Cengage Learning. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Instantiating the XML Controls (continued) 19 Android Boot Camp for Developers Using Java, 3rd Ed. 2016 Cengage Learning. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Writing Persistent Data with SharedPreferences 20 Android Boot Camp for Developers Using Java, 3rd Ed. Data is saved to an XML file as a keyvalue pair The key is a string such as key1 that uniquely identifies the preference The value is the data represented as a string, int, long, float, or Boolean Data types supported by the SharedPreferences class: putString( )Stores string values putInt( )Stores integer values putLong( )Stores long values putFloat( )Stores float values putBoolean( )Stores Boolean values 2016 Cengage Learning. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Writing Persistent Data with SharedPreferences (continued) 21 Android Boot Camp for Developers Using Java, 3rd Ed. 2016 Cengage Learning. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 22 Android Boot Camp for Developers Using Java, 3rd Ed. Writing Persistent Data with SharedPreferences (continued) 2016 Cengage Learning. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 23 Android Boot Camp for Developers Using Java, 3rd Ed. Launching the Second Activity 2016 Cengage Learning. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Instantiating the Second Activity Controls 24 Android Boot Camp for Developers Using Java, 3rd Ed. 2016 Cengage Learning. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Retrieving Preferences 25 Android Boot Camp for Developers Using Java, 3rd Ed. Retrieve the SharedPreferences object and use the appropriate method to retrieve a keys value by name getString( )Retrieves string values getInt( )Retrieves integer values getLong( )Retrieves long values getFloat( )Retrieves float values getBoolean( )Retrieves Boolean values 2016 Cengage Learning. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Retrieving Preferences (continued) 26 Android Boot Camp for Developers Using Java, 3rd Ed. 2016 Cengage Learning. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Retrieving Preferences (continued) 27 Android Boot Camp for Developers Using Java, 3rd Ed. 2016 Cengage Learning. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Coding an ImageView Control 28 Android Boot Camp for Developers Using Java, 3rd Ed. An ImageView control can display an image by assigning a source path (android:src="drawable/filename") in the XML layout file or by dynamically assigning the image within the Java code 2016 Cengage Learning. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Coding an ImageView Control (continued) 29 Android Boot Camp for Developers Using Java, 3rd Ed. 2016 Cengage Learning. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Coding an ImageView Control (continued) 30 Android Boot Camp for Developers Using Java, 3rd Ed. 2016 Cengage Learning. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Coding an ImageView Control (continued) 31 Android Boot Camp for Developers Using Java, 3rd Ed. 2016 Cengage Learning. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Summary When data users enter in an Android app is stored in RAM, it is lost when the app or the device stops running Persistent data, on the other hand, is stored on the devices drive or other storage medium such as a memory card or cloud service so that the data can be retrieved later within the app or after the termination of the program Persistent data can be stored using shared preferences, internal storage, external storage, a SQLite database, or a network connection Use the SharedPreferences object to save any primitive data: Booleans, floats, ints, longs, and strings 32 Android Boot Camp for Developers Using Java, 3rd Ed. 2016 Cengage Learning. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Summary (continued) When you save application data using the SharedPreferences object, the data is saved to an XML file as a keyvalue pair The key is a string such as key1 that uniquely identifies the preference, and the value is the data represented as a string, int, long, float, or Boolean. You can use the keyvalue pairs stored in SharedPreferences in different Activities of your application or in another application Use a putDataType( ) method to store the data in a SharedPreferences object, and use a getDataType( ) method to retrieve the data. 33 Android Boot Camp for Developers Using Java, 3rd Ed.


Recommended