Practice2: Master/Detail

Objectives

We are going to make an application that simulates a master detail list for a books, for this, from the template offered by Android Studio we will go modifying it to create our application.

We will use a file with the data of the application "datos_json.json ", therefore we will have to be able to read it and convert it into Kotlin objects . For this we will use the GSON library.

For those who don't know what JSON means, JSON stands for JavaScript Object Notation . It's an easy way to define objects and share them in plain text files like xml or yaml. Many APIs share data using JSON such as Firebase If you want to learn more about JSON click here

Before starting

Create a project type Master detail to start modifying it. Name it as P2_master_detail_books .




Copy the file datos_json.json inside the RAW folder inside RES

datos_json.json
[ {
        "id" : "0",
        "author" : "Harper Lee",
        "description" : "From Harper Lee comes a landmark new novel set two decades after her beloved Pulitzer Prize-winning masterpiece, To Kill a Mockingbird. Maycomb, Alabama. Twenty-six-year-old Jean Louise Finch--'Scout'--returns home from New York City to visit her aging father, Atticus. Set against the backdrop of the civil rights tensions and political turmoil that were transforming the South, Jean Louise's homecoming turns bittersweet when she learns disturbing truths about her close-knit family, the town and the people dearest to her. Memories from her childhood flood back, and her values and assumptions are thrown into doubt. Featuring many of the iconic characters from To Kill a Mockingbird, Go Set a Watchman perfectly captures a young woman, and a world, in a painful yet necessary transition out of the illusions of the past--a journey that can be guided only by one's conscience. Written in the mid-1950s, Go Set a Watchman imparts a fuller, richer understanding and appreciation of Harper Lee. Here is an unforgettable novel of wisdom, humanity, passion, humor and effortless precision--a profoundly affecting work of art that is both wonderfully evocative of another era and relevant to our own times. It not only confirms the enduring brilliance of To Kill a Mockingbird, but also serves as its essential companion, adding depth, context and new meaning to an American classic.",
        "publication_date" : "14/07/2015",
        "title" : "Go set a watchman",
        "url_image" : "watchman.jpg"
      }, {
        "id" : "1",
        "author" : "Paula Hawkins",
        "description" : "EVERY DAY THE SAME\n\nRachel takes the same commuter train every morning and night. Every day she rattles down the track, flashes past a stretch of cozy suburban homes, and stops at the signal that allows her to daily watch the same couple breakfasting on their deck. She’s even started to feel like she knows them. Jess and Jason, she calls them. Their life—as she sees it—is perfect. Not unlike the life she recently lost.\n\nUNTIL TODAY\n\nAnd then she sees something shocking. It’s only a minute until the train moves on, but it’s enough. Now everything’s changed. Unable to keep it to herself, Rachel goes to the police. But is she really as unreliable as they say? Soon she is deeply entangled not only in the investigation but in the lives of everyone involved. Has she done more harm than good?",
        "publication_date" : "13/01/2015",
        "title" : "The girl on the train",
        "url_image" : "hawkins.jpg"
      }, {
        "id" : "2",  
        "author" : "Kristin Hannah",
        "description" : "In the quiet village of Carriveau, Vianne Mauriac says goodbye to her husband, Antoine, as he heads for the Front. She doesn’t believe that the Nazis will invade France...but invade they do, in droves of marching soldiers, in caravans of trucks and tanks, in planes that fill the skies and drop bombs upon the innocent. When France is overrun, Vianne is forced to take an enemy into her house, and suddenly her every move is watched; her life and her child’s life is at constant risk. Without food or money or hope, as danger escalates around her, she must make one terrible choice after another.\n\nVianne’s sister, Isabelle, is a rebellious eighteen-year-old girl, searching for purpose with all the reckless passion of youth. While thousands of Parisians march into the unknown terrors of war, she meets the compelling and mysterious Gäetan, a partisan who believes the French can fight the Nazis from within France, and she falls in love as only the young can...completely. When he betrays her, Isabelle races headlong into danger and joins the Resistance, never looking back or giving a thought to the real--and deadly--consequences.\n\nWith courage, grace and powerful insight, bestselling author Kristin Hannah takes her talented pen to the epic panorama of WWII and illuminates an intimate part of history seldom seen: the women’s war. The Nightingale tells the stories of two sisters, separated by years and experience, by ideals, passion and circumstance, each embarking on her own dangerous path toward survival, love, and freedom in German-occupied, war-torn France--a heartbreakingly beautiful novel that celebrates the resilience of the human spirit and the durability of women. It is a novel for everyone, a novel for a lifetime.",
        "publication_date" : "3/02/2015",
        "title" : "The nightingale",
        "url_image" : "nightingale.png"
      }, {
        "id" : "3",
        "author" : "Neil Gaiman",
        "description" : "Trigger Warning explores the masks we all wear and the people we are beneath them to reveal our vulnerabilities and our truest selves. Here is a rich cornucopia of horror and ghosts stories, science fiction and fairy tales, fabulism and poetry that explore the realm of experience and emotion. In 'Adventure Story'--a thematic companion to The Ocean at the End of the Lane--Gaiman ponders death and the way people take their stories with them when they die. His social media experience 'A Calendar of Tales' are short takes inspired by replies to fan tweets about the months of the year--stories of pirates and the March winds, an igloo made of books, and a Mother's Day card that portends disturbances in the universe. Gaiman offers his own ingenious spin on Sherlock Holmes in his award-nominated mystery tale 'The Case of Death and Honey'. And 'Click-Clack the Rattlebag' explains the creaks and clatter we hear when we're all alone in the darkness.",
        "publication_date" : "3/02/2015",
        "title" : "Trigger Warning: short fictions",
        "url_image" : "triggerwarning.jpg"
      }, {
        "id" : "4",  
        "author" : "Pierce Brown",
        "description" : "With shades of The Hunger Games, Ender’s Game, and Game of Thrones, debut author Pierce Brown’s genre-defying epic Red Rising hit the ground running and wasted no time becoming a sensation. Golden Son continues the stunning saga of Darrow, a rebel forged by tragedy, battling to lead his oppressed people to freedom from the overlords of a brutal elitist future built on lies. Now fully embedded among the Gold ruling class, Darrow continues his work to bring down Society from within. A life-or-death tale of vengeance with an unforgettable hero at its heart, Golden Son guarantees Pierce Brown’s continuing status as one of fiction’s most exciting new voices.",
        "publication_date" : "06/01/2015",
        "title" : "Golden son",
        "url_image" : "goldenson.jpg"
      }, { 
        "id" : "5",  
        "author" : "Dean Koontz",
        "description" : "Two years after the cataclysmic events that sent him journeying into mystery, Odd Thomas, the intrepid fry cook who sees the dead and tries to help them, has traveled full circle, back to his beloved home town of Pico Mundo and the people he loves. He has come to save them-and perhaps humanity-from the full flowering of evil it is his destiny to confront, as he draws ever closer to the truth of the world and his place in it. Stronger, wiser than he started, and with the help of the friends he has made along the way, Odd prepares to confront the terrible forces arrayed against him and possibly to journey still farther, to his long-awaited reunion with his lost love, Stormy Llewellyn.",
        "publication_date" : "13/01/2015",
        "title" : "Saint odd",
        "url_image" : "saintodd.jpg"
      }, {  
        "id" : "6",  
        "author" : "Mindy Kaling",
        "description" : "In Why Not Me?, Kaling shares her ongoing journey to find contentment and excitement in her adult life, whether it’s falling in love at work, seeking new friendships in lonely places, attempting to be the first person in history to lose weight without any behavior modification whatsoever, or most important, believing that you have a place in Hollywood when you’re constantly reminded that no one looks like you.\n\nIn 'How to Look Spectacular: A Starlet’s Confessions,' Kaling gives her tongue-in-cheek secrets for surefire on-camera beauty, ('Your natural hair color may be appropriate for your skin tone, but this isn’t the land of appropriate–this is Hollywood, baby. Out here, a dark-skinned woman’s traditional hair color is honey blonde.') 'Player' tells the story of Kaling being seduced and dumped by a female friend in L.A. ('I had been replaced by a younger model. And now they had matching bangs.') In 'Unlikely Leading Lady', she muses on America’s fixation with the weight of actresses, (“Most women we see onscreen are either so thin that they’re walking clavicles or so huge that their only scenes involve them breaking furniture.”) And in 'Soup Snakes', Kaling spills some secrets on her relationship with her ex-boyfriend and close friend, B.J. Novak ('I will freely admit: my relationship with B.J. Novak is weird as hell.')\n\nMindy turns the anxieties, the glamour, and the celebrations of her second coming-of-age into a laugh-out-loud funny collection of essays that anyone who’s ever been at a turning point in their life or career can relate to. And those who’ve never been at a turning point can skip to the parts where she talks about meeting Bradley Cooper.",
        "publication_date" : "15/09/2015",
        "title" : "Why not me?",
        "url_image" : "bookshot1.png"
      }, { 
        "id" : "7",  
        "author" : "Aziz Ansari, Eric Klinenberg",
        "description" : "At some point, every one of us embarks on a journey to find love. We meet people, date, get into and out of relationships, all with the hope of finding someone with whom we share a deep connection. This seems standard now, but it’s wildly different from what people did even just decades ago. Single people today have more romantic options than at any point in human history. With technology, our abilities to connect with and sort through these options are staggering. So why are so many people frustrated?\n\nSome of our problems are unique to our time. 'Why did this guy just text me an emoji of a pizza?' 'Should I go out with this girl even though she listed Combos as one of her favorite snack foods? Combos?!' 'My girlfriend just got a message from some dude named Nathan. Who’s Nathan? Did he just send her a photo of his penis? Should I check just to be sure?'\n\nBut the transformation of our romantic lives can’t be explained by technology alone. In a short period of time, the whole culture of finding love has changed dramatically. A few decades ago, people would find a decent person who lived in their neighborhood. Their families would meet and, after deciding neither party seemed like a murderer, they would get married and soon have a kid, all by the time they were twenty-four. Today, people marry later than ever and spend years of their lives on a quest to find the perfect person, a soul mate.\n\nFor years, Aziz Ansari has been aiming his comic insight at modern romance, but for Modern Romance, the book, he decided he needed to take things to another level. He teamed up with NYU sociologist Eric Klinenberg and designed a massive research project, including hundreds of interviews and focus groups conducted everywhere from Tokyo to Buenos Aires to Wichita. They analyzed behavioral data and surveys and created their own online research forum on Reddit, which drew thousands of messages. They enlisted the world’s leading social scientists, including Andrew Cherlin, Eli Finkel, Helen Fisher, Sheena Iyengar, Barry Schwartz, Sherry Turkle, and Robb Willer. The result is unlike any social science or humor book we’ve seen before.\n\nIn Modern Romance, Ansari combines his irreverent humor with cutting-edge social science to give us an unforgettable tour of our new romantic world.",
        "publication_date" : "16/06/2015",
        "title" : "Modern Romance",
        "url_image" : "modernromance.jpg"
      }, { 
        "id" : "8",  
        "author" : "John Hargrove, Howard Chua",
        "description" : "Over the course of two decades, John Hargrove worked with 20 different whales on two continents and at two of SeaWorld's U.S. facilities. For Hargrove, becoming an orca trainer fulfilled a childhood dream. However, as his experience with the whales deepened, Hargrove came to doubt that their needs could ever be met in captivity. When two fellow trainers were killed by orcas in marine parks, Hargrove decided that SeaWorld's wildly popular programs were both detrimental to the whales and ultimately unsafe for trainers.\n\nAfter leaving SeaWorld, Hargrove became one of the stars of the controversial documentary Blackfish. The outcry over the treatment of SeaWorld's orca has now expanded beyond the outlines sketched by the award-winning documentary, with Hargrove contributing his expertise to an advocacy movement that is convincing both federal and state governments to act.\n\nIn Beneath the Surface, Hargrove paints a compelling portrait of these highly intelligent and social creatures, including his favorite whales Takara and her mother Kasatka, two of the most dominant orcas in SeaWorld. And he includes vibrant descriptions of the lives of orcas in the wild, contrasting their freedom in the ocean with their lives in SeaWorld.\n\nHargrove's journey is one that humanity has just begun to take-toward the realization that the relationship between the human and animal worlds must be radically rethought.",
        "publication_date" : "24/03/2015",
        "title" : "Beneath the surface",
        "url_image" : "beneaththesurface.jpg"
      } ]


Copy the images inside the drawable folder

BookItem class In Java

Here is a sample class in Java that you might need to model a book.

Remember that in Kotlin we hace Data classes .

public class BookItem {
    public final String id;
    public final String author;
    public final String description;
    public final String publication_date;
    public final String title;
    public final String url_image;
    public BookItem(String id, String author, String description,
        String publication_date, String title, String url_image) {
        this.id = id; this.author = author;
        this.description = description; this.publication_date =
        publication_date; this.title = title;
        this.url_image = url_image;
    }
    @Override
    public String toString() {
        return this.author + " " + this.title;
    }
    //Getters & Setters
}


GSON Library

Google provides us with a library known as GSON that makes our work easier with the JSON, parsing with it is like child's play. Add the dependency on the gradle.

dependencies {
        .....
        implementation 'com.google.code.gson:gson:2.10'
        .....
}


Now to create a Gson object add:

Gson gson = new Gson();


To use it and serialize it, if we had a model with the data whose class we have called BookItem, and a String with the JSON string called STRING_JSON , we can create an object coding something like this:

val bookItem: BookItem = gson.fromJson(STRING_JSON,BookItem::class.java)


However, if we have an object and we want a JSON, that is, deserialize, our class to extract the JSON document from its data, we would use:

val json = gson.toJson(bookItem)


In code above json variable would content an String more or less like this:

"{
    "id" : "4",
    "author" : "Pierce Brown",
    "description" : "With shades of The Hunger Games, Ender’s Game, and Game of Thrones, debut author Pierce Brown’s genre-defying epic Red Rising hit the ground running and wasted no time becoming a sensation. Golden Son continues the stunning saga of Darrow, a rebel forged by tragedy, battling to lead his oppressed people to freedom from the overlords of a brutal elitist future built on lies. Now fully embedded among the Gold ruling class, Darrow continues his work to bring down Society from within. A life-or-death tale of vengeance with an unforgettable hero at its heart, Golden Son guarantees Pierce Brown’s continuing status as one of fiction’s most exciting new voices.",
    "publication_date" : "06/01/2015",
    "title" : "Golden son",
    "url_image" : "goldenson.jpg"
}"


In our case, to read the data_json.json file that we have stored in the RAW folder inside RES and since our json file contains an array of json objects [{....},{....},{.....}] we can read it like this

var booksList:MutableList<BookItem>? = null
val raw = context.resources.openRawResource(R.raw.datos_json)
val rd = BufferedReader(InputStreamReader(raw))

val listType: Type = object : TypeToken<MutableList<BookItem?>?>() {}.type

val gson = Gson()
booksList = gson.fromJson(rd, listType)


Image from drawable to ImageView

We have downloaded our pictures inside drawable folder, and we only have its name through url_image property so we can fit those pictures inside an ImageView using the below code

val imageName = "imagename" //name of pictures without extension "jpg" , "png"...
val context: Context = imageView.getContext()
val id = context.resources.getIdentifier(
    imageName,
    "drawable",
    context.packageName
)
imageView.setImageResource(id)


App requirements

Initially, no items will appear in the list. It will be when we press the FloatingActionButton (you must have the add icon with background gray #AAAAAA), when pressed FloatingActionButton a SnackBar will appear. It will give us an action to load all BookItems into the RecyclerView .


Just launched


Snackbar with load actionp

Once clicked, the books will be loaded into the list reading them from the JSON file

banlibros.jog should be added to the actionBar



If we push on an Item then item's detail will be shown

Clicking on the FloatingActionButton will show a snackbar with the publication date.



In the tablet version, both fragments will be displayed on the same screen as shown in the image below.


Tablet Version

Demonstration

Mobile Version



Tablet Version



Delivery

Upload the project's container folder in a compressed zip file.