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:

  1. Adding Destinations to the Tour. We will allow users to click on the map to add new destinations to the map tour.
  2. 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
  • add and remove items from lists
  • use a Notifier for user input to an app
  • describe fundamental concepts of databases and data persistence
  • use a TinyDB database component to permanently save app data on a device
Language Objectives: I will be able to
  • explain what it means for data to persist in an app
  • use target vocabulary such as database while describing app features and UI components, with the support of concept definitions and vocabulary notes from this lesson

3.9.2. Learning Activities

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 Tutorial

To get started, you can use the app you created in the previous lesson and follow along with the video tutorial or the Text Tutorial or for an additional challenge, the Short Handout.

(Teacher Tube version)


Your instructor may ask you to do some or all of the following enhancements for your Map Tour with TinyDB app.
  1. 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.
  2. 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.
  3. 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).

3.9.3. Summary

In this lesson, you learned how to:

3.9.4. Self-Check


Here 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.

3.9.5. Reflection: For Your Portfolio

Answer the following portfolio reflection questions as directed by your instructor. Questions are also available in this Google Doc where you may use File/Make a Copy to make your own editable copy.

You have attempted of activities on this page