Code Challenge 3: Search a Database

Code Challenge 3

Search a Database

Description

Create an app that allows the user to search a database of information and view their results. The database should contain names and descriptions of famous people, places, movies, music, events or anything else you want. Make sure you have at least three entries in your database.

See our example app here: Technovation Challenge #3

Try to figure out how to complete the challenge on your own before looking at the instructions. The instructions show just one way of how this challenge can be solved. Happy coding!

Overview

We decided to tackle this coding challenge by storing names and descriptions of women scientists in a TinyDB. We used the scientist's name as the tag name to store the description of her work. After that we made two lists, one with the person’s name, and one with the descriptions of her work. The user will enter text and the app will search each item in both lists for a match. If there is a match, the woman’s name will appear in a listview. The user can then select which scientist they want to learn more about and a new screen will open with the description of her work. For this app, we had to use a TinyDB to store the data so that we could use it on separate screens.

Name (Tag) Description
Ada Lovelace Ada was an English mathematician and writer, who lived in the 1800s and is known for her work on Charles Babbage's early mechanical general-purpose computer, the Analytical Engine. Ada is often credited as being the first programmer ever because she wrote the first algorithm intended to be carried out by a machine.
Marie Curie Marie was a physicist and chemist who was born in Warsaw, Poland in 1867. With her husband Pierre, she discovered 2 new radioactive elements. Marie won the nobel prize in physics in 1903. She also discovered that radium gas could be used for cancer treatment.
Alice Ball Alice was a chemist born in Seattle in 1892. She was the first African American and the first woman to graduate from the University of Hawaii.  At only 23 years old, Alice developed a cure for leprosy, which prior to her work seemed like a hopeless disease.
Sau Lan Wu Sau Lan is a particle physicist who was born in the early 1940s during the Japanese occupation of Hong Kong. She earned her PhD at Harvard. She led the team that discovered gluon. She is one of the most important particle physicist in her field and has made many groundbreaking discoveries.
Patricia Bath Patricia is an ophthalmologist and inventor who was born in 1942 in Harlem, New York City. She finished high school in only 2 and a half years and knew she wanted to be a doctor. In 1985 she finished an invention that removes cataracts and restore people’s  sight around the world.

Designing the Screen

Our users need to enter text to search, so we are going to add a textbox for the user to enter their search and a button for them to click once they are done.  We know we are going to store our data in a database, so we dragged a TinyDB onto the screen. We also know that we’ll be using lists so we added a listview to the screen.

image17

Adding Data to the Database

We decided to add the scientist’s name as the tag and the description of her work as the value to the database. We added the information to the database like this:

image37

We added this information to the database when the screen initializes so it is the first thing our app does. We need to use a database in order to share information between screens. In App Inventor, the only two ways to pass data between screens is by using a database or the startValue block. You will see how both of those methods work in these instructions. You can read more about this here.

Creating lists of tag names and database values

We created a list to store the tag names in. First we created an empty list called “TagNames”. Next we added blocks to add every one of our tag names to this list. We made sure that the tag names being added to the list matched exactly the tag names in the database, including all capital letters.

image58

Now that we have a full list with all of our tag names, we need to create another list containing all of the values in the database. It's important that they are in the same order as the tag names. First, we created an empty list to hold all the values from the database called “databaseValues”. Next, we added each value to the list in the same order as the tag that corresponds to it by using a for loop. For each item in the “tagNames” list, we added the value from the database to the “databaseValues” list. We then added this for loop to the when Screen1.Initialize event handler.

image42

image50

Check point #1

At this point you should have all your data in your database as well as one list with your tag names and one list with your database values. Make sure that each of your lists contains the values you want! Since your app doesn’t do anything yet, you can check your work by displaying your lists in your ListView and making sure everything is there. Add this block to your code underneath your for loop in your when Screen1.Initialize event handler to check that your “tagNames” list is accurate.

image00

Add this block to your code underneath your for loop inside your when Screen1.Initialize event handler to check that your “databaseVales” list is accurate.

image48

After you’ve made sure your lists are accurate, makes sure to delete these blocks!

Check Point #2

You just wrote a lot of code! Check your code to make sure you can search for something. Try searching for a name, like “Alice” or an occupation like “chemist”. Make sure you get the results you expected to get before moving on!

Displaying the search results on a new screen

We decided our app would be better if the user could select the women scientist that showed up in the search results to learn more about her. To do this, when the user picked something from the list, we opened a new screen with a startValue of the user’s selection. Remember, the only two ways to pass data between screens is by using a database or a startValue block!

image31

We designed our second screen with two labels. One to display the scientist's name and one to display her description. We also added a back button to return to Screen1. Don’t forget that you also need to add a TinyDB to this screen!

image56

Since the start value is the name of the scientist, we displayed the start value in Label1 and also used it to retrieve the scientist’s description from the database to display in Label2. You can find the get start value block in the control category.

image06

Don’t forget to program your back button to bring you back to the first screen!

image11

When the user goes back, Screen1 Initializes again and everything starts over.

Final Check Point

Make sure everything in your app is working! Does selecting an item in the ListView bring you to Screen2 and display the tag name and database value?

Still stuck? Download our source code. Here are instructions on how to download and use source codes.