DbTradeAlert for Android: Add Security and Watchlist Management – Part 3

First post in this series: Introduction to DbTradeAlert

Previous post: DbTradeAlert for Android: Add Security and Watchlist Management – Part 2


2. Add Securities Management

Securities management works exactly the same way watchlists management does. And adding securities management will follow the exact same steps adding watchlist management used. So let’s just fast forward through this.

2.1 Add the Manage Securities Screen

You need a new empty activity named “SecuritiesManagementActivity” with 3 Buttons, a ListView, and a TextView similar to activity_watchlists_management.xml.

And you need code for the Cancel and OK buttons and to set the screen’s title in onCreate() like in WatchlistsManagementActivity.java.

And finally you need a new menu item in menu_watchlist_list.xml.

Then you need to extend WatchlistListActivity to start the new activity in onOptionsItemSelected() and retrieve its result in onActivityResult().

Manage Securities screen - list of watchlists empty

Manage Securities screen – list of watchlists empty

Now try out the additions:

  1. Start the app
  2. In its overflow menu tap “Manage Securities”
  3. The “Manage Securities” screen appears
    1. Note the hint about adding a new security when there is none
    2. Note that there is no menu but a distinct title for the screen
  4. In the “Manage Securities” screen tap either OK or Cancel – tapping “New” will crash the app
  5. Optional: commit the changes

2.2 Fill List of Existing Securities

To list the existing securities DbTradeAlert needs a layout to show each security. It’s added like layout_watchlists_management_detail with one more TextView to show the security’s symbol.

Manage Securities screen listing securities

Manage Securities screen listing securities

The next step will be using an adapter to marry the ListView with its cursor and its detail layout. So create a new class named “SecuritiesManagementCursorAdapter” extending CursorAdapter like you did for WatchlistsManagementCursorAdapter.

For explanations see filling the list of existing watchlists.

Finally connect SecuritiesManagementCursorAdapter to securitiesListView in WatchlistsManagementActivity.onCreate() – to fill the list of securities DbHelper.getAllSecuritiesAndMarkIfInWatchlist() will be reused.

Test it: the securities show up in the Manage Securities screen. Optionally check in the changes.

2.3 Add an Activity to Edit Securities

Add a new empty activity named “SecurityEditActivity”. It hosts a lot of controls and I’ll just show the resulting layout:

Edit Security layout

Edit Security layout

The first step after creating the layout is to provide a handler for SecuritiesManagementActivity’s New button. The pattern is exactly like in WatchlistsManagementActivity:

  1. Create an intent pointing to SecurityEditActivity.class
  2. Add an Extra named SecurityEditActivity.SECURITY_ID_INTENT_EXTRA with the security ID of DbHelper.NEW_ITEM_ID to the intent
  3. Call startActivityForResult() passing the intent and SecurityEditActivity.CREATE_SECURITY_REQUEST_CODE

And SecurityEditActivity’s required extensions:

  1. It needs definitions for CREATE_SECURITY_REQUEST_CODE and SECURITY_ID_INTENT_EXTRA
  2. onCancelButtonClick():
    1. Sets result to RESULT_CANCELED
    2. Closes the screen
  3. onCreate():
    1. Grabs the ID from the intent’s Extras
    2. Clears the EditTexts for new securities
    3. Sets the screen’s title
    4. Calls refreshSecuritiesListView() passing the security’s ID
  4. refreshSecuritiesListView():
    1. Calls dbHelper.getAllWatchlistsAndMarkIfSecurityIsIncluded() passing the security’s ID
    2. Creates a SimpleCursorAdapter instance to connect cursor and ListView
    3. Sets the ListView’s EmptyView
    4. Marks the watchlists that include the currently shown security

Again this code works exactly like that for editing a watchlist – just with a lot more fields.

Finally DbHelper needs getAllWatchlistsAndMarkIfSecurityIsIncluded() which works like explained for DbHelper.getAllSecuritiesAndMarkIfInWatchlist().

Run the app again:

  1. Open the Manage Securities screen
  2. Tap New: the new Edit Security screen shows up listing all watchlists and ready for input
    1. again no menu but a distinct title of “Add Security”
  3. Tap Cancel – tapping OK will crash the app
  4. Optional: commit changes
Adding a new security

Adding a new security

The screen shows a security’s fields: at the top is the symbol field. Below that you see value and date for base and maximum price, targets, and the notes field. Note that the dates have to be entered according to the device’s locale. While Android provides a DatePickerDialog I found it too much hassle to implement and connect it to the EditTexts.

At the bottom of the screen is a list of all watchlists with those checked that contain this security.

Note that that I closed the automatically displayed on-screen keyboard for the screenshot so it doesn’t obstruct the activity’s screen.

Next post: DbTradeAlert for Android: Add Security and Watchlist Management – Part 4

Additional Resources:

Advertisements
This entry was posted in Uncategorized and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s