Unformatted text preview:

Notepad Exercise 1 In this exercise, you will construct a simple notes list that lets the user add new notes but not edit them. The exercise demonstrates: • The basics of ListActivities and creating and handling menu options. • How to use a SQLite database to store the notes. • How to bind data from a database cursor into a ListView using a SimpleCursorAdapter. • The basics of screen layouts, including how to lay out a list view, how you can add items to the activity menu, and how the activity handles those menu selections. [Exercise 1] [Exercise 2] [Exercise 3] [Extra Credit] Step 1 Open up the Notepadv1 project in Eclipse. Notepadv1 is a project that is provided as a starting point. It takes care of some of the boilerplate work that you have already seen if you followed the Hello, World tutorial. 1. Start a new Android Project by clicking File > New > Android Project. 2. In the New Android Project dialog, select Create project from existing source. 3. Click Browse and navigate to where you copied the NotepadCodeLab (downloaded during setup) and select Notepadv1. 4. The Project Name and other properties should be automatically filled for you. You must select the Build Target—we recommend selecting a target with the lowest platform version available. Also add an integer to the Min SDK Version field that matches the API Level of the selected Build Target. 5. Click Finish. The Notepadv1 project should open and be visible in your Eclipse package explorer. If you see an error about AndroidManifest.xml, or some problems related to an Android zip file, right click on the project and select Android Tools > Fix Project Properties. (The project is looking in the wrong location for the library file, this will fix it for you.) Step 2 Accessing and modifying data For this exercise, we are using a SQLite database to store our data. This is useful if only your application will need to access or modify the data. If you wish for other activities to access or modify the data, you have to expose the data using a ContentProvider. If you are interested, you can find out more about content providers or the whole subject of Data Storage. The NotePad sample in the samples/ folder of the SDK also has an example of how to create a ContentProvider. Take a look at the NotesDbAdapter class — this class is provided to encapsulate data access to a SQLite database that will hold our notes data and allow us to update it. At the top of the class are some constant definitions that will be used in the application to look up data from the proper field names in the database. There is also a database creation string defined, which is used to create a new database schema if one doesn't exist already. Our database will have the name data, and have a single table called notes, which in turn has three fields: _id, title and body. The _id is named with an underscore convention used in a number of places inside the Android SDK and helps keep a track of state. The _id usually has to be specified when querying or updating the database (in the column projections and so on). The other two fields are simple text fields that will store data.The constructor for NotesDbAdapter takes a Context, which allows it to communicate with aspects of the Android operating system. This is quite common for classes that need to touch the Android system in some way. The Activity class implements the Context class, so usually you will just pass this from your Activity, when needing a Context. The open() method calls up an instance of DatabaseHelper, which is our local implementation of the SQLiteOpenHelper class. It calls getWritableDatabase(), which handles creating/opening a database for us. close() just closes the database, releasing resources related to the connection. createNote() takes strings for the title and body of a new note, then creates that note in the database. Assuming the new note is created successfully, the method also returns the row _id value for the newly created note. deleteNote() takes a rowId for a particular note, and deletes that note from the database. fetchAllNotes() issues a query to return a Cursor over all notes in the database. The query() call is worth examination and understanding. The first field is the name of the database table to query (in this case DATABASE_TABLE is "notes"). The next is the list of columns we want returned, in this case we want the _id, title and body columns so these are specified in the String array. The remaining fields are, in order: selection, selectionArgs, groupBy, having and orderBy. Having these all null means we want all data, need no grouping, and will take the default order. See SQLiteDatabase for more details. Note: A Cursor is returned rather than a collection of rows. This allows Android to use resources efficiently -- instead of putting lots of data straight into memory the cursor will retrieve and release data as it is needed, which is much more efficient for tables with lots of rows. fetchNote() is similar to fetchAllNotes() but just gets one note with the rowId we specify. It uses a slightly different version of the SQLiteDatabase query() method. The first parameter (set true) indicates that we are interested in one distinct result. The selection parameter (the fourth parameter) has been specified to search only for the row "where _id =" the rowId we passed in. So we are returned a Cursor on the one row. And finally, updateNote() takes a rowId, title and body, and uses a ContentValues instance to update the note of the given rowId. Step 3 Layouts and activities Most Activity classes will have a layout associated with them. The layout will be the "face" of the Activity to the user. In this case our layout will take over the whole screen and provide a list of notes. Full screen layouts are not the only option for an Activity however. You might also want to use a floating layout (for example, a dialog or alert), or perhaps you don't need a layout at all (the Activity will be invisible to the user unless you specify some kind of layout for it to use). Open the notepad_list.xml file in res/layout and take a look at it. (You may have to hit the xml tab, at the bottom, in order to view the XML markup.) This is a mostly-empty layout definition file. Here are some things you should know about a layout file: • All Android layout files must start with the XML header line: <?xml version="1.0" encoding="utf-8"?>. • The next definition will often (but


View Full Document

SMU CSE 7392 - Study Guide

Documents in this Course
Load more
Download Study Guide
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Study Guide and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Study Guide 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?