Mobile App Development with MIT App Inventor
Managing Data with Variables and Lists
Storing and Managing Data
Apps aren't very useful if they can't remember things. Whether it's a user's name, a high score, or a list of tasks, your app needs a way to store and manage data. The most basic tool for this is a variable.
Think of a variable as a labeled box where you can keep a piece of information. You can look inside the box to see what's there, or you can replace the contents with something new.
Variable
noun
A container for storing a piece of data. It has a name and holds a value, which can change or 'vary' as the app runs.
In App Inventor, you create variables in the Blocks editor. You start by giving your variable a name and an initial value. This is called initializing the variable. You'll use the initialize global variable block found under the Variables drawer.
Once you have a variable, you can change its value using the set block and read its value using the get block. This is how you'll store user input. For example, you can take the text from a TextBox component and set it as the value of your playerName variable.
Working with Lists
A single variable is great for storing one piece of information, like a name or a score. But what if you need to store many pieces of information? For that, you use a list.
A list is a special type of variable that can hold multiple values in a specific order. You can create a shopping list, a list of friends, or a list of tasks for a to-do app.
You create a list much like a regular variable, but you use the create empty list block from the Lists drawer. This sets up an empty container that you can add items to later.
Once you have your list, you can perform several actions on it.
| Block | Purpose |
|---|---|
add items to list | Adds a new item to the end of the list. |
select list item | Gets an item from the list at a specific position (index). |
remove list item | Deletes an item from the list at a specific index. |
length of list | Returns how many items are currently in the list. |
A Simple To-Do App
Let's combine these ideas. Imagine an app with a TextBox for a new task, a Button to add the task, and a Label to display the list.
First, you'd initialize a global variable called taskList to an empty list.
When the user clicks the 'Add Task' button, you need to do two things:
- Add the text from the
TextBoxto yourtaskList. - Update the
Labelto show the new contents of the list.
With these blocks, every time the button is clicked, the new task is added to your list, and the label immediately updates to show the entire list. You've successfully stored user input and managed it in a collection.
Ready to test your knowledge? Try this short quiz.
In App Inventor, what is the primary purpose of the initialize global variable block?
You have a TextBox component named UserInput and a global variable named playerName. Which block combination correctly stores the text from the TextBox into the playerName variable?
Variables and lists are the building blocks for creating dynamic apps that can react to user input and handle complex information.