A list (also called an array in other programming languages) is a tool that can be used to store multiple pieces of information at once.
In this tutorial, we will learn how to create, navigate and find items in a List. For this, we will create an Android app that will parse text (e.g. user entry text) and will then encode or decode it using Lists. We will also use DRY (Don’t Repeat Yourself) concept by using Procedures.
This tutorial assumes that user is already familiar with App Inventor Or AppyBuilder. This app is being developed using AppyBuilder, but same concept can be used in App Inventor. Source code can be downloaded using link at bottom of this post and it can be imported into FREE AppyBuilder here: http://FREE.AppyBuilder.com.
This tutorial focuses on behaviour / logic rather than user interface and for this, it does not contain any user interface. You can tailor this code to your needs and design the user interface as needed.
We will use Procedure to include the processing logic. However, before diving into blocks logic, take a look at image below that shows results of calling the procedure. The procedure-calls below encodes “apple peach” and then decodes it back to original text.
We now start coding the logic and start by creating three global variables:
- varTemp – used to get result of encoding or decoding of text
- listFrom and listTo – These synchronized lists will contain a mapping of letters. Details on usage will be covered below.
Next, we create a Procedure. This procedure will return result. It will also take 2 parameters as shown below:
- shouldEncode – this boolean parameter is used to indicate if we want the procedure to encode or decode text
- someText – this parameter will be used to receive the text that procedure should encode or decode
Next, from Control drawer, we drag & drop a “do with result” block. We use this block so that we include additional blocks and also return a result when procedure completes. Plug the “do with results” into procedure return socket as shown below:
Image below shows entire code that is used for this encodeOrDecode procedure and below explains logic for each section.
- A – In this section, we first change the passed in text as lower case. We also replace all spaces with pipe (|). The reason for this is that some list-blocks ignore blank space. So we temporarily change space to pipe and when returning final result, change pipe back to space.
- B – In this section, we initialize our listFrom and listTo to some default values in format of CSV and then use “list from csv row” to convert CSV to list. NOTE: Complete the list to include all alphabet letters
- C – Our procedure is used to encode OR decode. If the parameter shouldEncode is false, we switch our listFrom and listTo lists
- D – We initialize varTemp to empty string. This variable will contain the final result
- E – We include a “for each” loop that will be repeated for the length of passed-in text. Inside this block, we will inspect each letter of the text by using the index of the loop.
- F – Reading blocks from right-to-left, we retrieve the letter at position idx (segment), find its position (index) in listFrom (index in list), then retrieve letter at position idx of listTo (select list item). Then we append this to varTemp
- G – In this section, we take our final varTemp, replace all the pipe (|) back to spaces and then return that as final result.
Blocks below show how to invoke above procedure and encoding or decoding text.
Source and .apk
- Follow link below using your device to install the .apk: http://AppyBuilder.com/tutorials/encodeDecode/encodeDecode.apk
- Download source (.aia) using link here: http://AppyBuilder.com/tutorials/encodeDecode/encodeDecode.aia
Like this post? Share using button below and give us your thumbs-up