Download presentation
Presentation is loading. Please wait.
Published byElmer Jacobs Modified over 9 years ago
1
Address Book App 1
2
Define styles res/values/address_book_styles.xml @drawable/textview_border Specify a background for a TextView – res/drawable/textview_border.xml – The textview_border is defined as a shape element 2
3
Create menu resources res/menu – Define the MenuItems The menu will be inflated by Activity’s MenuInflater getMenuInflater().inflate(R.menu.add ressbook_menu, menu); 2
4
Extend ListActivity A ListActivity ’s default GUI consists of a ListView (we do not need to define a separate layout) – Specify the format that’s applied to each list item shown in the ListView (contact_list_item.xml) Create an Activity extends from ListActivity lv = getListView(); //get the built-in ListView lv.setOnItemClickListener(oicl); 5
5
ListView ListView is a subclass of AdapterView, a GUI component is bound to a data source via an Adapter. We use CursorAdapter to display the results of a database query in the ListView. 5
6
CursorAdapter SimpleCursorAdapter is a subclass of CursorAdapter that’s designed to mapping Cursor columns directly to TextView or ImageView To create a SimpleCursonAdapter, you must first define arrays containing the column names to map to GUI components ( contact_list_item.xml ) and the resource IDs ( R.id.contactTV ) of the GUI components that will display the data from the named columns. String[] columns = {"name"}; int[] components = {R.id.contactTV}; contactAdapter = new SimpleCursorAdapter(this, R.layout.contact_list_item, null, columns, components, 0); lv.setAdapter(contactAdapter); 5
7
CursorAdapter SimpleCursorAdapter requires 6 arguments: – Context in which the ListView is running – 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 5
8
AsyncTask Perform action in one thread and receive the results in GUI thread Three parameters: – Type of the parameter for doInBackground method, which is performed in separate thread – Type of the parameter for onProgressUpdate method, which is executed in the GUI thread and is used to receive intermediate updates of the specified type from a long- running task – Type of the parameter for onPostExecute method, which is executed in the GUI thread and enables the Activity to use the AsyncTask ’s results. 6
9
AsyncTask example GetContactTask extends AsyncTask 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
10
OnResume Get the complete list of contacts from the database and make CursorAdpater works – (new GetContactTask()).execute((Object[ ]) null); – execute do not receive any argument in this case 5
11
OnStop CursorAdpater Cursor is not needed contactAdapter.changeCursor(null); //adapter now has no cursor 5
12
SQLite Database DatabaseConnector – 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 7
13
DatabaseOpenHelper Extends from SQLiteOpenHelper – Helps apps create databases and manage version changes Constructor requires 4 arguments – Context – Database name – CursorFactory = null indicates use default SQLite CursorFactory – Database version number 7
14
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
15
Database Operations 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+””}); 7
16
Query method database.query(7 arguments) – 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 getAllContacts – db.query("contacts", columns, null, null, null, null, null); getOneContact(long id) – db.query("contacts", columns, "_id=?", new String[]{id+””}, null, null, null); 7
17
ViewContact LoadContactTask extends AsyncTask doInBaground – Open database – getOneContact(params[0]) onPostExecute – 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(); 7
18
DeleteContact Show alert dialog DeleteAsync extends AsyncTask doInBaground – Open database – deleteContact(params[0]) onPostExecute – finish(); //return to the AddressBook Activity 1. Dismiss any dialogs the activity was managing. 2. Close any cursors the activity was managing. 3. Close any open search dialog 7
19
EditContact SaveContactTask extends AsyncTask doInBaground – Open database – updateContact onPostExecute – finish() 7
20
AddContact SaveContactTask extends AsyncTask doInBaground – Open database – insertContact onPostExecute – finish() Show alert dialog if name field is null 7
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.