Variáveis e listas no App Inventor

  • Learn how to use variables to store data within an app
  • Learn how to organize data into lists
  • Use variables and lists in an app

Estas são as atividades para esta lição:

STORING DATA

When you code an app, there usually is some information or data that you need to store, update, and access in the app.

There are 3 types of data you might need for running an app:

Variables

Store information while the app runs.

For example, a username or a game score.

Armazenamento local

Armazenar informações no dispositivo móvel para serem recuperadas sempre que o aplicativo for executado.

Por exemplo, um endereço de usuário ou uma pontuação alta em um jogo.

Armazenamento em nuvem

Armazenar informações na Web (nuvem) para que todos os usuários do aplicativo possam acessar as informações.

Por exemplo, tabela de classificação de jogos ou mensagens de bate-papo.

In this lesson, we’ll focus on storing data within the app, using variables.

VARIABLES

One of the most important concepts in coding is a variable. A variable is a name for a piece of data that can change. An easy way to remember is that a variable can vary, or change in value.

There’s lots of information that is important to you every day that changes value. Here are some examples:

birthday cake with candles
your age
clouds with raindrops
the weather
monthly calendar
today's date

A variable is like a box where you can store information inside. 

  1. You label or name the box to remember what is inside.
  2. When you need the information you can open the box and get what is inside
  3. You can also change what is inside the box but keep the same name outside.

 

animation of variable being stored in a box

VARIÁVEIS DO APP INVENTOR

Variáveis são usadas para armazenar informações durante a execução do aplicativo.

Todos os blocos de variáveis são encontrados na gaveta Variables (Variáveis). 

app inventor gaveta de variáveis

There are three basic blocks for variables. Learn about them in the tabs below.

The initialize block sets aside storage in your app for your variable. Give it a useful name.

 

bloco de inicializar no App Inventor

Here a variable numberOfLessons is initialized to 0, perhaps to track how many lessons have been completed in an education app.

The set block lets you set, or update, the value of the variable.

 

Here, the current value is gotten and the variable is set to the current value + 1. So, if numberOfLessons was 2, the new value would be 3.

The get block lets you get the current value of the variable.

Here, the get block is used to insert the number of lessons into a sentence to display in Label1.
bloco get variable usado no bloco de junção
 
Here it is used to get the current number of lessons to test if it is zero, in a conditional block.
bloco if App inventor
 
 
 

LISTS

As you build your app, you might find you have multiple pieces of data that need to be accessed within the app.

For example, instead of one username, your app might need to access all the usernames for the app. Or perhaps your app is all about animals, so there are multiple animal names and images that must be stored.

No App Inventor, você usa algo chamado lista para organizar vários itens de dados em seus programas. Outras linguagens de programação as chamam de arraysmas elas são essencialmente a mesma coisa.

You may have made a to-do list or a grocery list before, and lists in programming are very similar.

shopping list on paper with pen on top
paleta de lista no app inventor

As listas são acessadas por meio da paleta Blocos incorporados no Blocks Editor. Há vários blocos que permitem criar, armazenar, recuperar e atualizar itens em uma lista.

Você armazena uma lista em uma variável, portanto, pode dar um nome à sua lista.

inicializando uma lista no app inventor

You should use lists anytime you have multiple pieces of information that are related.

For example, if you wanted to display the top 10 high scores for a game, you could make ten variables with names like HighScore1, HighScore2, HighScore3 and so on until you get to HighScore10.

The preferred action is to make a list called HighScores to hold all 10 high scores. Ordering, updating, and adding high scores is much easier with a single list.

variáveis vs listas em app inventor

Each item in a list has an index, which is its place in the list, to allow you to retrieve the item from the list.

For example, in the blocks below, the index of 2 points to the 2nd item in the Fruits list. So, Label1 will display “Bananas”.

inicializando uma lista no app inventor
selecionando em uma lista

You can also add, remove, and replace things in lists. 

If you took your original Fruits list of [Apples, Bananas, Oranges] can you guess what the Fruits list will contain after you run the blocks below, in the order they are in?

inicializando uma lista no app inventor

Best practices: The word variable is one of the harder topics for students to learn. The box analogy really helps so if you have to do some real life labeling of boxes to illustrate things, that might be a good idea!

Guiding Questions to ask students:

Do you think there are other ways to store data other than lists or variables? What about if you want to keep two values together? Like I want a list of how many oranges and how many apples I have, something like so:
Oranges: 3
Apples: 5

I want to store this somehow. It’s a more complicated list. It’s almost like a list where the values are linked together like so:
Oranges -> 3
Apples -> 5

The arrows have a direction kind of so we would call this a map! There are plenty of other data structures too! Can you find any others?

Advanced knowledge for students who are excelling: If one of your students is interested in doing more coding that isn’t block coding (ex: continuing to code in Python or Javascript) you could explain to them that coding languages care more about what is in the box. For example, numbers are handled differently than characters in a word. Languages like Python and Javascript are very picky about how you store numbers vs characters because they are different “types” of things you can store in your variable. You could make the analogy that you use different types of boxes to store things: Your leftovers have to go in a special box for leftovers (tupperware) and then when you are moving, your stuff has to go in a moving box (cardboard box). The same is for python. Numbers go into numbers boxes and characters go into characters boxes. 

As dicas são fornecidas com o apoio do AmeriCorps.

A estilizado, logotipo da AmeriCorps em azul-marinho

ACTIVITY: COMPLETE THE QUIZ APP

Tempo estimado: 45 minutos

Complete the code using variables and lists

Follow the directions in the worksheet to make a quiz app that tests the user's knowledge on a topic of your choice.
Open Worksheet

REFLEXÃO

 

In this lesson, you learned how to store information in your app, and how to use lots of information with lists. Consider these questions:

reflection with bits of data
What data will you need to store in your app?
How might you include lists in your app?

REVISÃO DOS PRINCIPAIS TERMOS

Variable– a way to name and access information within an app

Listas- uma maneira de organizar várias partes de dados no App Inventor

Index  –  a number that tells you where a piece of data is in a list 

Array – common name for lists in programming languages other than App Inventor and Thunkable

RECURSOS ADICIONAIS

O componente ListView permite:

  • display of list elements in your app
  • user selection of an item in the list.
  • coding of an action to take when the user selects an item

Watch this video for a quick demo of it in use: