Difference between revisions of "Teams Winter 2011/team5/lab5/tutorial"
Abhishekbh (talk | contribs) |
Abhishekbh (talk | contribs) |
||
Line 4: | Line 4: | ||
=== Database Helper === | === Database Helper === | ||
+ | Add in the following code: | ||
+ | |||
+ | <pre> | ||
+ | |||
+ | package dps914.team5.lab3; | ||
+ | |||
+ | import android.content.ContentValues; | ||
+ | import android.content.Context; | ||
+ | import android.database.Cursor; | ||
+ | import android.database.SQLException; | ||
+ | import android.database.sqlite.SQLiteDatabase; | ||
+ | import android.database.sqlite.SQLiteException; | ||
+ | import android.database.sqlite.SQLiteOpenHelper; | ||
+ | import android.util.Log; | ||
+ | |||
+ | public class DAL { | ||
+ | private static final String TAG = DAL.class.getSimpleName(); | ||
+ | |||
+ | private static final String DATABASE_NAME = "mybooks.db"; | ||
+ | private static final String DATABASE_TABLE = "mybooks_table"; | ||
+ | private static final int DATABASE_VERSION = 1; | ||
+ | private static final String CREATE_TABLE = "create table " + DATABASE_TABLE | ||
+ | + " (_id integer primary key autoincrement, " + "name text not null," | ||
+ | + "author text," + "publisher text," + "category text" + ");"; | ||
+ | |||
+ | private Context ctx; | ||
+ | private SQLiteDatabase db; | ||
+ | private OpenHelper oh; | ||
+ | |||
+ | public static final String KEY_NAME = "name"; | ||
+ | public static final String KEY_AUTHOR = "email_address"; | ||
+ | public static final String KEY_PUBLISHER = "mobile"; | ||
+ | public static final String KEY_CATEGORY = "home_address"; | ||
+ | public static final String KEY_ROW_ID = "_id"; | ||
+ | |||
+ | private static final String QUERY_NAME = "SELECT _id, name from " | ||
+ | + DATABASE_TABLE + " WHERE " + KEY_NAME + " LIKE ?"; | ||
+ | |||
+ | public DAL(Context context) { | ||
+ | ctx = context; | ||
+ | } | ||
+ | |||
+ | public DAL open() { | ||
+ | try { | ||
+ | oh = new OpenHelper(ctx); | ||
+ | db = oh.getWritableDatabase(); | ||
+ | return this; | ||
+ | } catch (SQLException e) { | ||
+ | Log.d(TAG, "OPEN: failed "); | ||
+ | return null; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | public void close() { | ||
+ | oh.close(); | ||
+ | } | ||
+ | |||
+ | public Long createBook(String name, String emailAddress, String mobile, | ||
+ | String homeAddress) { | ||
+ | ContentValues initialValues = new ContentValues(); | ||
+ | initialValues.put(KEY_NAME, name); | ||
+ | initialValues.put(KEY_AUTHOR, emailAddress); | ||
+ | initialValues.put(KEY_PUBLISHER, mobile); | ||
+ | initialValues.put(KEY_CATEGORY, homeAddress); | ||
+ | return db.insert(DATABASE_TABLE, null, initialValues); | ||
+ | } | ||
+ | |||
+ | public void deleteBook(long rowId) { | ||
+ | db.delete(DATABASE_TABLE, KEY_ROW_ID + "=" + rowId, null); | ||
+ | } | ||
+ | |||
+ | public Cursor getAllBooks() { | ||
+ | try { | ||
+ | |||
+ | return db.query(DATABASE_TABLE, new String[]{KEY_ROW_ID, KEY_NAME, | ||
+ | KEY_AUTHOR, KEY_PUBLISHER, KEY_CATEGORY}, null, null, null, null, | ||
+ | null); | ||
+ | |||
+ | } catch (SQLiteException e) { | ||
+ | Log.e(TAG, "Exception on query ALL BOOKS"); | ||
+ | return null; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | public Cursor getBook(long rowId) throws SQLException { | ||
+ | |||
+ | Cursor c = db.query(true, DATABASE_TABLE, new String[]{KEY_ROW_ID, | ||
+ | KEY_NAME, KEY_AUTHOR, KEY_PUBLISHER, KEY_CATEGORY}, KEY_ROW_ID + "=" | ||
+ | + rowId, null, null, null, null, null); | ||
+ | if (c != null) { | ||
+ | c.moveToFirst(); | ||
+ | } | ||
+ | return c; | ||
+ | |||
+ | } | ||
+ | |||
+ | public Cursor getAllBooks(String name) { | ||
+ | try { | ||
+ | Cursor c = db.rawQuery(QUERY_NAME, new String[]{name}); | ||
+ | return c; | ||
+ | |||
+ | } catch (SQLiteException e) { | ||
+ | Log.e(TAG, "Exception on query ALL BOOKS"); | ||
+ | return null; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | public boolean updateBook(long rowId, String name, String emailAddress, | ||
+ | String mobile, String homeAddress) { | ||
+ | ContentValues cv = new ContentValues(); | ||
+ | cv.put(KEY_NAME, name); | ||
+ | cv.put(KEY_AUTHOR, emailAddress); | ||
+ | cv.put(KEY_PUBLISHER, mobile); | ||
+ | cv.put(KEY_CATEGORY, homeAddress); | ||
+ | return db.update(DATABASE_TABLE, cv, KEY_ROW_ID + "=" + rowId, null) > 0; | ||
+ | } | ||
+ | |||
+ | private static class OpenHelper extends SQLiteOpenHelper { | ||
+ | |||
+ | public OpenHelper(Context context) { | ||
+ | super(context, DATABASE_NAME, null, DATABASE_VERSION); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void onCreate(SQLiteDatabase db) { | ||
+ | Log.d(TAG, "onCreate sql " + CREATE_TABLE); | ||
+ | db.execSQL(CREATE_TABLE); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | ||
+ | db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE); | ||
+ | onCreate(db); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | } | ||
+ | |||
+ | </pre> | ||
=== First Activity Code - Book Manager === | === First Activity Code - Book Manager === | ||
+ | <pre> | ||
+ | |||
+ | package dps914.team5.lab3; | ||
+ | |||
+ | import android.app.ListActivity; | ||
+ | import android.content.Intent; | ||
+ | import android.database.Cursor; | ||
+ | import android.os.Bundle; | ||
+ | import android.util.Log; | ||
+ | import android.view.ContextMenu; | ||
+ | import android.view.ContextMenu.ContextMenuInfo; | ||
+ | import android.view.Menu; | ||
+ | import android.view.MenuInflater; | ||
+ | import android.view.MenuItem; | ||
+ | import android.view.View; | ||
+ | import android.widget.AdapterView.AdapterContextMenuInfo; | ||
+ | import android.widget.ListAdapter; | ||
+ | import android.widget.SimpleCursorAdapter; | ||
+ | |||
+ | public class team5lab extends ListActivity { | ||
+ | private DAL h; | ||
+ | private Cursor c; | ||
+ | private static final String TAG = team5lab.class.getSimpleName(); | ||
+ | private static final int ACTIVITY_CREATE = 0; | ||
+ | private static final int ACTIVITY_EDIT = 1; | ||
+ | |||
+ | /** Called when the activity is first created. */ | ||
+ | @Override | ||
+ | public void onCreate(Bundle savedInstanceState) { | ||
+ | super.onCreate(savedInstanceState); | ||
+ | setContentView(R.layout.show_books); | ||
+ | h = new DAL(team5lab.this); | ||
+ | h.open(); | ||
+ | displayData(); | ||
+ | registerForContextMenu(getListView()); | ||
+ | } | ||
+ | |||
+ | private void displayData() { | ||
+ | c = h.getAllBooks(); | ||
+ | startManagingCursor(c); | ||
+ | ListAdapter adapter = new SimpleCursorAdapter(this, | ||
+ | R.layout.book_row, c, new String[] { | ||
+ | DAL.KEY_NAME, DAL.KEY_AUTHOR, | ||
+ | DAL.KEY_ROW_ID }, new int[] { R.id.name, R.id.author }); | ||
+ | setListAdapter(adapter); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public boolean onCreateOptionsMenu(Menu menu) { | ||
+ | MenuInflater inflater = getMenuInflater(); | ||
+ | inflater.inflate(R.menu.create_menu, menu); | ||
+ | return super.onCreateOptionsMenu(menu); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public boolean onOptionsItemSelected(MenuItem item) { | ||
+ | |||
+ | switch (item.getItemId()) { | ||
+ | case R.id.book_create: | ||
+ | Log.d(TAG, "onOptItemSelected-create"); | ||
+ | Intent i = new Intent(this, EditBookActivity.class); | ||
+ | startActivityForResult(i, ACTIVITY_CREATE); | ||
+ | break; | ||
+ | } | ||
+ | return super.onOptionsItemSelected(item); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void onCreateContextMenu(ContextMenu menu, View v, | ||
+ | ContextMenuInfo menuInfo) { | ||
+ | super.onCreateContextMenu(menu, v, menuInfo); | ||
+ | MenuInflater inflater = getMenuInflater(); | ||
+ | inflater.inflate(R.menu.context_menu, menu); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public boolean onMenuItemSelected(int featureId, MenuItem item) { | ||
+ | switch (item.getItemId()) { | ||
+ | |||
+ | case R.id.book_delete: | ||
+ | AdapterContextMenuInfo info = (AdapterContextMenuInfo) item | ||
+ | .getMenuInfo(); | ||
+ | h.deleteBook(info.id); | ||
+ | displayData(); | ||
+ | return true; | ||
+ | |||
+ | case R.id.book_update: | ||
+ | Intent i = new Intent(this, EditBookActivity.class); | ||
+ | AdapterContextMenuInfo infoItem = (AdapterContextMenuInfo) item | ||
+ | .getMenuInfo(); | ||
+ | i.putExtra(DAL.KEY_ROW_ID, infoItem.id); | ||
+ | startActivityForResult(i, ACTIVITY_EDIT); | ||
+ | Log.d(TAG, "Update Contact Info"); | ||
+ | return true; | ||
+ | } | ||
+ | return super.onMenuItemSelected(featureId, item); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | protected void onActivityResult(int requestCode, int resultCode, | ||
+ | Intent intent) { | ||
+ | super.onActivityResult(requestCode, resultCode, intent); | ||
+ | displayData(); | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
== First Activity Layout == | == First Activity Layout == | ||
+ | <pre> | ||
+ | |||
+ | <?xml version="1.0" encoding="utf-8"?> | ||
+ | <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
+ | android:orientation="vertical" android:layout_width="fill_parent" | ||
+ | android:layout_height="fill_parent"> | ||
+ | |||
+ | <TextView android:layout_width="wrap_content" | ||
+ | android:layout_height="wrap_content" android:id="@+id/titleTextView" | ||
+ | android:text="@string/titleTextView" android:textStyle="bold" | ||
+ | android:textSize="20dp" android:gravity="center_vertical" | ||
+ | android:layout_gravity="center"></TextView> | ||
+ | |||
+ | <LinearLayout android:orientation="horizontal" | ||
+ | android:layout_width="fill_parent" android:layout_height="wrap_content"> | ||
+ | |||
+ | <EditText android:id="@+id/searchText" android:layout_width="fill_parent" | ||
+ | android:layout_height="wrap_content" android:layout_weight="1" /> | ||
+ | |||
+ | <Button android:id="@+id/searchButton" android:layout_width="wrap_content" | ||
+ | android:layout_height="wrap_content" android:text="@string/searchButton" /> | ||
+ | |||
+ | </LinearLayout> | ||
+ | <ListView android:layout_width="fill_parent" | ||
+ | android:layout_height="fill_parent" android:id="@+id/android:list"/> | ||
+ | |||
+ | </LinearLayout> | ||
+ | </pre><br/> | ||
+ | |||
+ | And a list layout:<br/> | ||
+ | |||
+ | <pre> | ||
+ | |||
+ | <?xml version="1.0" encoding="utf-8"?> | ||
+ | |||
+ | <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
+ | android:layout_width="fill_parent" android:layout_height="fill_parent" | ||
+ | android:layout_gravity="fill_horizontal"> | ||
+ | |||
+ | <TextView android:id="@+id/name" android:text="@string/view_name" | ||
+ | android:layout_gravity="left" android:height="30dp" | ||
+ | android:layout_height="wrap_content" android:layout_width="wrap_content" | ||
+ | android:layout_weight=".60" /> | ||
+ | |||
+ | <TextView android:layout_height="wrap_content" android:id="@+id/author" | ||
+ | android:text="@string/view_author" android:layout_width="wrap_content" | ||
+ | android:layout_gravity="right" android:layout_weight=".40" /> | ||
+ | </LinearLayout> | ||
+ | |||
+ | </pre> | ||
== First Activity Menus == | == First Activity Menus == | ||
+ | |||
+ | <pre> | ||
+ | |||
+ | <?xml version="1.0" encoding="utf-8"?> | ||
+ | |||
+ | <menu xmlns:android="http://schemas.android.com/apk/res/android"> | ||
+ | <item android:id="@+id/book_create" android:title="Create Book"></item> | ||
+ | |||
+ | </menu> | ||
+ | |||
+ | </pre> | ||
== First Activity Context Menu == | == First Activity Context Menu == | ||
+ | |||
+ | <pre> | ||
+ | |||
+ | <?xml version="1.0" encoding="utf-8"?> | ||
+ | |||
+ | <menu xmlns:android="http://schemas.android.com/apk/res/android"> | ||
+ | <item android:title="@string/menu_update" android:id="@+id/book_update" /> | ||
+ | <item android:title="@string/menu_delete" android:id="@+id/book_delete" /> | ||
+ | |||
+ | </menu> | ||
+ | |||
+ | </pre> | ||
=== Edit Activity Code === | === Edit Activity Code === | ||
+ | <pre> | ||
+ | |||
+ | package dps914.team5.lab3; | ||
+ | |||
+ | import android.app.Activity; | ||
+ | import android.database.Cursor; | ||
+ | import android.os.Bundle; | ||
+ | import android.util.Log; | ||
+ | import android.view.View; | ||
+ | import android.view.View.OnClickListener; | ||
+ | import android.widget.Button; | ||
+ | import android.widget.EditText; | ||
+ | |||
+ | public class EditBookActivity extends Activity { | ||
+ | private final static String TAG = EditBookActivity.class.getSimpleName(); | ||
+ | |||
+ | private DAL h; | ||
+ | private Long rowId; | ||
+ | |||
+ | private String name; | ||
+ | private String emailAddress; | ||
+ | private String phone; | ||
+ | private String homeAddress; | ||
+ | |||
+ | private EditText nameEditText; | ||
+ | private EditText emailAddressEditText; | ||
+ | private EditText phoneEditText; | ||
+ | private EditText homeAddressEditText; | ||
+ | private Button saveButton; | ||
+ | |||
+ | @Override | ||
+ | protected void onCreate(Bundle savedInstanceState) { | ||
+ | super.onCreate(savedInstanceState); | ||
+ | setContentView(R.layout.edit_book); | ||
+ | h = new DAL(EditBookActivity.this); | ||
+ | h.open(); | ||
+ | |||
+ | getWidgets(); | ||
+ | |||
+ | rowId = (savedInstanceState == null) ? null : (Long) savedInstanceState | ||
+ | .getSerializable(DAL.KEY_ROW_ID); | ||
+ | if (rowId == null) { | ||
+ | Bundle extras = getIntent().getExtras(); | ||
+ | rowId = extras != null | ||
+ | ? extras.getLong(DAL.KEY_ROW_ID) | ||
+ | : null; | ||
+ | } | ||
+ | |||
+ | fillContactInfo(); | ||
+ | |||
+ | saveButton.setOnClickListener(new OnClickListener() { | ||
+ | public void onClick(View arg0) { | ||
+ | setResult(RESULT_OK); | ||
+ | finish(); | ||
+ | } | ||
+ | |||
+ | }); | ||
+ | } | ||
+ | |||
+ | private void fillContactInfo() { | ||
+ | if (rowId != null) { | ||
+ | Cursor c = h.getBook(rowId); | ||
+ | startManagingCursor(c); | ||
+ | nameEditText.setText(c.getString(c | ||
+ | .getColumnIndexOrThrow(DAL.KEY_NAME))); | ||
+ | emailAddressEditText.setText(c.getString(c | ||
+ | .getColumnIndexOrThrow(DAL.KEY_AUTHOR))); | ||
+ | phoneEditText.setText(c.getString(c | ||
+ | .getColumnIndexOrThrow(DAL.KEY_PUBLISHER))); | ||
+ | homeAddressEditText.setText(c.getString(c | ||
+ | .getColumnIndexOrThrow(DAL.KEY_CATEGORY))); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | protected void onSaveInstanceState(Bundle outState) { | ||
+ | super.onSaveInstanceState(outState); | ||
+ | saveState(); | ||
+ | outState.putSerializable(DAL.KEY_ROW_ID, rowId); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | protected void onPause() { | ||
+ | super.onPause(); | ||
+ | saveState(); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | protected void onResume() { | ||
+ | super.onResume(); | ||
+ | fillContactInfo(); | ||
+ | } | ||
+ | |||
+ | private void saveState() { | ||
+ | name = nameEditText.getText().toString(); | ||
+ | emailAddress = emailAddressEditText.getText().toString(); | ||
+ | phone = phoneEditText.getText().toString(); | ||
+ | homeAddress = homeAddressEditText.getText().toString(); | ||
+ | |||
+ | if (rowId == null) { | ||
+ | long id = h.createBook(name, emailAddress, phone, homeAddress); | ||
+ | if (id > 0) { | ||
+ | rowId = id; | ||
+ | } | ||
+ | Log.d(TAG, "INSERT ROW " + rowId); | ||
+ | } else { | ||
+ | h.updateBook(rowId, name, emailAddress, phone, homeAddress); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | private void getWidgets() { | ||
+ | nameEditText = (EditText) findViewById(R.id.name_editText); | ||
+ | emailAddressEditText = (EditText) findViewById(R.id.author_editText); | ||
+ | phoneEditText = (EditText) findViewById(R.id.publisher_editText); | ||
+ | homeAddressEditText = (EditText) findViewById(R.id.category_editText); | ||
+ | saveButton = (Button) findViewById(R.id.saveContact_button); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </pre> | ||
== Edit Activity Layouts == | == Edit Activity Layouts == | ||
+ | |||
+ | <pre> | ||
+ | |||
+ | <?xml version="1.0" encoding="utf-8"?> | ||
+ | <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
+ | android:layout_width="match_parent" android:layout_height="match_parent" | ||
+ | android:orientation="vertical"> | ||
+ | <TextView android:layout_width="wrap_content" | ||
+ | android:layout_height="wrap_content" android:id="@+id/titleTextView" | ||
+ | android:gravity="center_vertical" android:textSize="30dp" | ||
+ | android:text="" android:layout_gravity="center"></TextView> | ||
+ | |||
+ | |||
+ | <EditText android:layout_width="match_parent" | ||
+ | android:layout_height="wrap_content" android:id="@+id/name_editText" | ||
+ | android:hint="Title" android:layout_marginBottom="3dp"></EditText> | ||
+ | |||
+ | |||
+ | <EditText android:layout_width="match_parent" | ||
+ | android:layout_height="wrap_content" android:id="@+id/author_editText" | ||
+ | android:hint="Author" android:isScrollContainer="true" | ||
+ | android:layout_marginBottom="3dp"></EditText> | ||
+ | |||
+ | |||
+ | <EditText android:layout_width="match_parent" | ||
+ | android:layout_height="wrap_content" android:id="@+id/publisher_editText" | ||
+ | android:layout_marginBottom="3dp" android:hint="Publisher"></EditText> | ||
+ | |||
+ | |||
+ | <EditText android:layout_width="match_parent" | ||
+ | android:layout_height="wrap_content" android:id="@+id/category_editText" | ||
+ | android:layout_marginBottom="3dp" android:hint="Category"></EditText> | ||
+ | |||
+ | |||
+ | <Button android:layout_width="wrap_content" | ||
+ | android:layout_height="wrap_content" android:id="@+id/saveContact_button" | ||
+ | android:text="Save" android:gravity="center_horizontal" | ||
+ | android:layout_gravity="center_horizontal" android:textSize="20dp"></Button> | ||
+ | </LinearLayout> | ||
+ | |||
+ | </pre> |
Latest revision as of 14:14, 16 April 2011
Contents
Create Android Project
1.1 Setup Eclipse with the ADT plugin as directed to ttp://www.vogella.de/articles/Android/article.html#installation here]
1.2. Create a new android project in your eclipse: File -> New -> Android Project:
Database Helper
Add in the following code:
package dps914.team5.lab3; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteException; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class DAL { private static final String TAG = DAL.class.getSimpleName(); private static final String DATABASE_NAME = "mybooks.db"; private static final String DATABASE_TABLE = "mybooks_table"; private static final int DATABASE_VERSION = 1; private static final String CREATE_TABLE = "create table " + DATABASE_TABLE + " (_id integer primary key autoincrement, " + "name text not null," + "author text," + "publisher text," + "category text" + ");"; private Context ctx; private SQLiteDatabase db; private OpenHelper oh; public static final String KEY_NAME = "name"; public static final String KEY_AUTHOR = "email_address"; public static final String KEY_PUBLISHER = "mobile"; public static final String KEY_CATEGORY = "home_address"; public static final String KEY_ROW_ID = "_id"; private static final String QUERY_NAME = "SELECT _id, name from " + DATABASE_TABLE + " WHERE " + KEY_NAME + " LIKE ?"; public DAL(Context context) { ctx = context; } public DAL open() { try { oh = new OpenHelper(ctx); db = oh.getWritableDatabase(); return this; } catch (SQLException e) { Log.d(TAG, "OPEN: failed "); return null; } } public void close() { oh.close(); } public Long createBook(String name, String emailAddress, String mobile, String homeAddress) { ContentValues initialValues = new ContentValues(); initialValues.put(KEY_NAME, name); initialValues.put(KEY_AUTHOR, emailAddress); initialValues.put(KEY_PUBLISHER, mobile); initialValues.put(KEY_CATEGORY, homeAddress); return db.insert(DATABASE_TABLE, null, initialValues); } public void deleteBook(long rowId) { db.delete(DATABASE_TABLE, KEY_ROW_ID + "=" + rowId, null); } public Cursor getAllBooks() { try { return db.query(DATABASE_TABLE, new String[]{KEY_ROW_ID, KEY_NAME, KEY_AUTHOR, KEY_PUBLISHER, KEY_CATEGORY}, null, null, null, null, null); } catch (SQLiteException e) { Log.e(TAG, "Exception on query ALL BOOKS"); return null; } } public Cursor getBook(long rowId) throws SQLException { Cursor c = db.query(true, DATABASE_TABLE, new String[]{KEY_ROW_ID, KEY_NAME, KEY_AUTHOR, KEY_PUBLISHER, KEY_CATEGORY}, KEY_ROW_ID + "=" + rowId, null, null, null, null, null); if (c != null) { c.moveToFirst(); } return c; } public Cursor getAllBooks(String name) { try { Cursor c = db.rawQuery(QUERY_NAME, new String[]{name}); return c; } catch (SQLiteException e) { Log.e(TAG, "Exception on query ALL BOOKS"); return null; } } public boolean updateBook(long rowId, String name, String emailAddress, String mobile, String homeAddress) { ContentValues cv = new ContentValues(); cv.put(KEY_NAME, name); cv.put(KEY_AUTHOR, emailAddress); cv.put(KEY_PUBLISHER, mobile); cv.put(KEY_CATEGORY, homeAddress); return db.update(DATABASE_TABLE, cv, KEY_ROW_ID + "=" + rowId, null) > 0; } private static class OpenHelper extends SQLiteOpenHelper { public OpenHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { Log.d(TAG, "onCreate sql " + CREATE_TABLE); db.execSQL(CREATE_TABLE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE); onCreate(db); } } }
First Activity Code - Book Manager
package dps914.team5.lab3; import android.app.ListActivity; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.util.Log; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.ListAdapter; import android.widget.SimpleCursorAdapter; public class team5lab extends ListActivity { private DAL h; private Cursor c; private static final String TAG = team5lab.class.getSimpleName(); private static final int ACTIVITY_CREATE = 0; private static final int ACTIVITY_EDIT = 1; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.show_books); h = new DAL(team5lab.this); h.open(); displayData(); registerForContextMenu(getListView()); } private void displayData() { c = h.getAllBooks(); startManagingCursor(c); ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.book_row, c, new String[] { DAL.KEY_NAME, DAL.KEY_AUTHOR, DAL.KEY_ROW_ID }, new int[] { R.id.name, R.id.author }); setListAdapter(adapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.create_menu, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.book_create: Log.d(TAG, "onOptItemSelected-create"); Intent i = new Intent(this, EditBookActivity.class); startActivityForResult(i, ACTIVITY_CREATE); break; } return super.onOptionsItemSelected(item); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.context_menu, menu); } @Override public boolean onMenuItemSelected(int featureId, MenuItem item) { switch (item.getItemId()) { case R.id.book_delete: AdapterContextMenuInfo info = (AdapterContextMenuInfo) item .getMenuInfo(); h.deleteBook(info.id); displayData(); return true; case R.id.book_update: Intent i = new Intent(this, EditBookActivity.class); AdapterContextMenuInfo infoItem = (AdapterContextMenuInfo) item .getMenuInfo(); i.putExtra(DAL.KEY_ROW_ID, infoItem.id); startActivityForResult(i, ACTIVITY_EDIT); Log.d(TAG, "Update Contact Info"); return true; } return super.onMenuItemSelected(featureId, item); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { super.onActivityResult(requestCode, resultCode, intent); displayData(); } }
First Activity Layout
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/titleTextView" android:text="@string/titleTextView" android:textStyle="bold" android:textSize="20dp" android:gravity="center_vertical" android:layout_gravity="center"></TextView> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <EditText android:id="@+id/searchText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" /> <Button android:id="@+id/searchButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/searchButton" /> </LinearLayout> <ListView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/android:list"/> </LinearLayout>
And a list layout:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="fill_horizontal"> <TextView android:id="@+id/name" android:text="@string/view_name" android:layout_gravity="left" android:height="30dp" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight=".60" /> <TextView android:layout_height="wrap_content" android:id="@+id/author" android:text="@string/view_author" android:layout_width="wrap_content" android:layout_gravity="right" android:layout_weight=".40" /> </LinearLayout>
First Activity Menus
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/book_create" android:title="Create Book"></item> </menu>
First Activity Context Menu
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:title="@string/menu_update" android:id="@+id/book_update" /> <item android:title="@string/menu_delete" android:id="@+id/book_delete" /> </menu>
Edit Activity Code
package dps914.team5.lab3; import android.app.Activity; import android.database.Cursor; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class EditBookActivity extends Activity { private final static String TAG = EditBookActivity.class.getSimpleName(); private DAL h; private Long rowId; private String name; private String emailAddress; private String phone; private String homeAddress; private EditText nameEditText; private EditText emailAddressEditText; private EditText phoneEditText; private EditText homeAddressEditText; private Button saveButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.edit_book); h = new DAL(EditBookActivity.this); h.open(); getWidgets(); rowId = (savedInstanceState == null) ? null : (Long) savedInstanceState .getSerializable(DAL.KEY_ROW_ID); if (rowId == null) { Bundle extras = getIntent().getExtras(); rowId = extras != null ? extras.getLong(DAL.KEY_ROW_ID) : null; } fillContactInfo(); saveButton.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { setResult(RESULT_OK); finish(); } }); } private void fillContactInfo() { if (rowId != null) { Cursor c = h.getBook(rowId); startManagingCursor(c); nameEditText.setText(c.getString(c .getColumnIndexOrThrow(DAL.KEY_NAME))); emailAddressEditText.setText(c.getString(c .getColumnIndexOrThrow(DAL.KEY_AUTHOR))); phoneEditText.setText(c.getString(c .getColumnIndexOrThrow(DAL.KEY_PUBLISHER))); homeAddressEditText.setText(c.getString(c .getColumnIndexOrThrow(DAL.KEY_CATEGORY))); } } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); saveState(); outState.putSerializable(DAL.KEY_ROW_ID, rowId); } @Override protected void onPause() { super.onPause(); saveState(); } @Override protected void onResume() { super.onResume(); fillContactInfo(); } private void saveState() { name = nameEditText.getText().toString(); emailAddress = emailAddressEditText.getText().toString(); phone = phoneEditText.getText().toString(); homeAddress = homeAddressEditText.getText().toString(); if (rowId == null) { long id = h.createBook(name, emailAddress, phone, homeAddress); if (id > 0) { rowId = id; } Log.d(TAG, "INSERT ROW " + rowId); } else { h.updateBook(rowId, name, emailAddress, phone, homeAddress); } } private void getWidgets() { nameEditText = (EditText) findViewById(R.id.name_editText); emailAddressEditText = (EditText) findViewById(R.id.author_editText); phoneEditText = (EditText) findViewById(R.id.publisher_editText); homeAddressEditText = (EditText) findViewById(R.id.category_editText); saveButton = (Button) findViewById(R.id.saveContact_button); } }
Edit Activity Layouts
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/titleTextView" android:gravity="center_vertical" android:textSize="30dp" android:text="" android:layout_gravity="center"></TextView> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/name_editText" android:hint="Title" android:layout_marginBottom="3dp"></EditText> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/author_editText" android:hint="Author" android:isScrollContainer="true" android:layout_marginBottom="3dp"></EditText> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/publisher_editText" android:layout_marginBottom="3dp" android:hint="Publisher"></EditText> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/category_editText" android:layout_marginBottom="3dp" android:hint="Category"></EditText> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/saveContact_button" android:text="Save" android:gravity="center_horizontal" android:layout_gravity="center_horizontal" android:textSize="20dp"></Button> </LinearLayout>