Address Book App 1 Fall 2014 CS7020: Game Design and Development Specify additonal activity 2 AndroidManifest.xml file describes an app’s components. Each activity must be described in the app’s manifest Fall 2014 CS7020: Game Design and Development Define styles res/values/address_book_styles.xml <item name="android:background">@drawable/textview_border </item> Specify a background for a TextView – – 2 res/drawable/textview_border.xml The textview_border is defined as a shape element Fall 2014 CS7020: Game Design and Development Create menu resources res/menu – 2 Define the MenuItems The menu will be inflated by Activity’s MenuInflater getMenuInflater().inflate(R.menu.add ressbook_menu, menu); Fall 2014 CS7020: Game Design and Development Extend ListActivity Create an Activity that contains a ListView lv = getListView(); //get the built-in ListView lv.setOnItemClickListener(oicl); 5 Fall 2014 CS7020: Game Design and Development CursorAdapter CursorAdapter objest is used to display results in a ListView SimpleCursorAdapter is a subclass of CursorAdapter that’s designed to mapping Cursor columns directly to TextView String[] columns = {"name"}; int[] components = {R.id.contactTV}; contactAdapter = new SimpleCursorAdapter(this, R.layout.contact_list_item, null, columns, components, 0); lv.setAdapter(contactAdapter); SimpleCursorAdapter requires 6 arguments 5 – Context – Resource ID of the layout – The Cursor that provides access to the data – String array containing the column names to display – Int array containing the corresponding GUI resource ID – flag Fall 2014 CS7020: Game Design and Development OnResume 5 Get the complete list of contacts from the database and make CursorAdpater works AsyncTask – Execute task in a separate thread – (new GetContactTask()).execute((Object[ ]) null); – execute do not receive any argument in this case Fall 2014 CS7020: Game Design and Development OnStop 5 CursorAdpater Cursor is not needed contactAdapter.changeCursor(null); //adapter now has no cursor Fall 2014 CS7020: Game Design and Development AsynTask Three parameters: Type of the parameter for doInBackground method Type of the parameter for onProgressUpdate method Type of the parameter for onPostExecute method GetContactTask extends AsyncTask<Object, Object, Cursor> private DatabaseConnector dbc; protected Cursor doInBackground(Object... params) { dbc = new DatabaseConnector(AddressBook.this); dbc.open(); return dbc.getAllContacts();} protected void onPostExecute(Cursor result) { contactAdapter.changeCursor(result); dbc.close(); super.onPostExecute(result);} 6 Fall 2014 CS7020: Game Design and Development SQLite Database DatabaseConnector – – 7 Database names must be unique within a specific app but need not be unique across apps. A SQLiteDatabase object provides read/write access to a SQLite database DatabaseOpenHelper extends SQLiteOpenHelper is used to manage creating, opening and upgrading databases Fall 2014 CS7020: Game Design and Development DatabaseOpenHelper Extends from SQLiteOpenHelper – Constructor requires 4 arguments – – – – 7 Helps apps create databases and manage version changes Context Database name CursorFactory = null indicates use default SQLite CursorFactory Database version number Fall 2014 CS7020: Game Design and Development DatabaseOpenHelper OnCreate – Create the “contacts” table contains an integer primary key field (_id) that us auto-incremented, and 5 other text fields – String cmd = "create table contacts " +"(" + "_id integer primary key autoincrement, " + "name text, " + "email text, " + "phone text, " + "street text, " + "city text " + ")"; db.execSQL(cmd); //execute the query – – – – – 7 Fall 2014 CS7020: Game Design and Development Database Operations 7 open close insertContacts – ContentValue: key-value pairs – insert(db name, nullColumnHack, data) updateContact – db.update("contacts", cv, "_id=?", new String[]{id+""}); deleteContact(long id) – db.delete("contacts", "_id=?", new String[]{id+””}); Fall 2014 CS7020: Game Design and Development Query method database.query(7 arguments) – – – – – – – getAllContacts – db.query("contacts", columns, null, null, null, null, null); getOneContact(long id) – 7 Database name A String array of the column names to return A SQL WHERE clause A argument for WHERE clause A SQL GROUP BY clause A SQL HAVING clause A SQL ORDER BY clause db.query("contacts", columns, "_id=?", new String[]{id+””}, null, null, null); Fall 2014 CS7020: Game Design and Development ViewContact LoadContactTask extends AsyncTask<Long, Object, Cursor> doInBaground – – onPostExecute – – – – – – – – 7 Open database getOneContact(params[0]) result.moveToFirst(); //move to the first item nameTV.setText(result.getString(0)); phoneTV.setText(result.getString(1)); emailTV.setText(result.getString(2)); streetTV.setText(result.getString(3)); cityTV.setText(result.getString(4)); result.close(); dbc.close(); Fall 2014 CS7020: Game Design and Development DeleteContact Show alert dialog DeleteAsync extends AsyncTask<Long, Object, Object> doInBaground – – Open database deleteContact(params[0]) onPostExecute – finish(); //return to the AddressBook Activity 1. 2. 3. 7 Dismiss any dialogs the activity was managing. Close any cursors the activity was managing. Close any open search dialog Fall 2014 CS7020: Game Design and Development EditContact SaveContactTask extends AsyncTask<Object, Object, Object> doInBaground – – onPostExecute – 7 Open database updateContact finish() Fall 2014 CS7020: Game Design and Development AddContact SaveContactTask extends AsyncTask<Object, Object, Object> doInBaground – – onPostExecute – 7 Open database insertContact finish() Show alert dialog if name field is null Fall 2014 CS7020: Game Design and Development