3.9. Map Tour With TinyDB¶
Time Estimate: 45 minutes
3.9.1. Introduction and Goals¶
In this lesson we will extend the Map Tour App by adding two new features:
- Adding Destinations to the Tour. We will allow users to click on the map to add new destinations to the map tour.
- Data Persistence. We will incorporate TinyDB, MIT App Inventor's database component, which will enable the app to save new destinations for the user. Locations that are added to the destinations list will be saved to the database and re-loaded into the app when it starts up again.
(Teacher Tube version)
Learning Objectives: I will learn to
Language Objectives: I will be able to
3.9.2. Learning Activities¶
- short handout
- YouTube video
- TeacherTube video
- YouTube video - Tiny DB
- TeacherTube video - Tiny DB
What is TinyDb?
Up until now, the data in our apps has been stored either in global variables or as the value of the properties of the app’s various components. For example, when you store a piece of text in a Label, that data is stored in the computer’s main memory, in its RAM — random access memory. And as we’ve learned, RAM is volatile, meaning that any data stored there will be destroyed when the app is exited.
By contrast, data stored in the computer’s long-term storage — e.g., on the phone’s flash drive — will persist as long as the app is kept on the device. There are various ways to store data permanently on a computer. For example, you could store it in a file, such as a document or image file. Another way to store persistent data is in a database. MIT App Inventor provides us a very simple, easy-to-use database in its TinyDb component. Any data that we store in the TinyDb, will not disappear when the app is exited. Instead, it will persist between uses of the app -- even if you turn off the device.
Before working on incorporating TinyDb into our app, the following video provides a brief overview of this very important component. (Teacher Tube version)
Map Tour with TinyDB TutorialTeacher Tube version)
EnhancementsYour instructor may ask you to do some or all of the following enhancements for your Map Tour with TinyDB app.
- Text To Speech: Add a TextToSpeech component to the UI, and when the user picks an item from the list, call TextToSpeech.speak to say the selected item.
Delete Locations: As you are testing your app, you may have added a lot of locations on your map tour that you do not want. You could delete the data stored for the installed app in your device under Settings/Applications Settings or by calling TinyDB.clearAll in your code, but in this enhancement you will add a Delete ListPicker button that lets you choose a location to remove from your lists and update the database. Here are the steps you need to do:
- Add a ListPicker to the UI to Delete destinations.
- In ListPicker.BeforePicking, set the ListPicker.Elements to the destinations list.
- In ListPicker.AfterPicking, use the remove list item block from the Lists drawer to remove the item at the ListPicker.SelectedIndex from both of the lists (destinations and destinationsLatLong). Save both lists in TinyDB. Use Notifier.Alert to tell the user the destination was deleted.
- Refactor your code to add a saveToDB procedure to save both lists in TinyDB and call it from ListPicker.AfterPicking and Notifier.AfterTextInput.
- Add My Location: If you have a device and location where GPS works. when you click on the My Location block, add that location to the destinationsLatLong lists using the Add Item to List block and use the Notifier.ShowTextDialog to get the location name for the destinations list (this will call the already written Notifier.AfterTextInput procedure).
In this lesson, you learned how to:
VocabularyHere is a table of the technical terms introduced in this lesson. Hover over the terms to review the definitions.
Check Your Understanding
Complete the following self-check exercises.