Build an app which will parse data from a CSV source and store it in a list of models. It will display a list of names in the first activity and then show the details in the second.
Be sure to build and test your app at the end of each stage. You may need to add test code in your main activity to test some early features. ie. After you finish writing the
getAppListingsmethod, call it from yourAppRepositoryfrom the main activity. Add breakpoints and make sure all the data ended up where you expected it to be.
- Create a new Android Studio project
- Name the project "App List"
- Make sure that the target API level is below that of your testing environment.
- Open the "Mock Data" file in a text editor.
- Look at the headers
- Build a model class called
AppListingto store those values and use the header names as guided for your variable names - Be sure to have your data members be private and to generate getters and setters
- Create a class called
AppRepository - Add a final string value called
APP_DATA - Write a static method in that class called
getAppListingswhich will return anArrayListofAppListingobjects - In this method, you'll use (String.split)[https://developer.android.com/reference/java/lang/String.html#split(java.lang.String)] to parse the data from a CSV string.
-
Call the split method on your
APP_DATAdata member and pass it the newline character (APP_DATA.split('\n')) and store the result inappStrings -
Create an
ArrayListofAppListingobjects and initialize it with a newArrayList -
Write a for each loop to loop through
appStrings- In each loop call
spliton the provided string and pass it a comma character',' - Store that result as a String Array (
String[]) - Build a new
AppListingby grabbing the values you need at their indicies in the array.
You can run the debug tool with breakpoint right after the second split to see which values are at which index in your array
- Add your new listing to the
ArrayListcreated previously
- In each loop call
-
Check the data in your array and make sure you remove any element built with the header values
-
Return the
ArrayList
-
- Open the app's activity_main.xml file.
- Add a
ScrollViewto list all previously viewed images. Give it a linear layout child.
Set the
orientationattribute tovertical
- Add IDs to all the views that will need to be accessed in the code.
- In your
TextViewgenerator give theTextViewobject a listener that will get the tag from the element, use that to pull theImageDataobject from theList. - Use
intent.putExtra()to add the object to the intent
- In your main activity, call your repository's
getAppListingsmethod and store the result - Iterate through the list using a for each loop, build a text view with each element and add it to the view.
- Design a layout that will display details about the app.
- Use
getIntent()to get the intent used to launch this activity. - Use the
intent.getSerializableExtra()method to retreive the serialized data and then cast it toImageData.
- the final line of code should look something like this
myObject = (ImageData)intent.getSerializableExtra("KEY")
- Get the data from the object and display it in the view.