First post in this series: Introduction to DbTradeAlert
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().
Now try out the additions:
- Start the app
- In its overflow menu tap “Manage Securities”
- The “Manage Securities” screen appears
- Note the hint about adding a new security when there is none
- Note that there is no menu but a distinct title for the screen
- In the “Manage Securities” screen tap either OK or Cancel – tapping “New” will crash the app
- 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.
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:
The first step after creating the layout is to provide a handler for SecuritiesManagementActivity’s New button. The pattern is exactly like in WatchlistsManagementActivity:
- Create an intent pointing to SecurityEditActivity.class
- Add an Extra named SecurityEditActivity.SECURITY_ID_INTENT_EXTRA with the security ID of DbHelper.NEW_ITEM_ID to the intent
- Call startActivityForResult() passing the intent and SecurityEditActivity.CREATE_SECURITY_REQUEST_CODE
And SecurityEditActivity’s required extensions:
- It needs definitions for CREATE_SECURITY_REQUEST_CODE and SECURITY_ID_INTENT_EXTRA
- Sets result to RESULT_CANCELED
- Closes the screen
- Grabs the ID from the intent’s Extras
- Clears the EditTexts for new securities
- Sets the screen’s title
- Calls refreshSecuritiesListView() passing the security’s ID
- Calls dbHelper.getAllWatchlistsAndMarkIfSecurityIsIncluded() passing the security’s ID
- Creates a SimpleCursorAdapter instance to connect cursor and ListView
- Sets the ListView’s EmptyView
- 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:
- Open the Manage Securities screen
- Tap New: the new Edit Security screen shows up listing all watchlists and ready for input
- again no menu but a distinct title of “Add Security”
- Tap Cancel – tapping OK will crash the app
- Optional: commit changes
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.