Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Android Lists and Adapters (2015) Using Parcelable Data Parceling Data

Jeffrey Kwok
Jeffrey Kwok
3,006 Points

I'm having difficulty parsing a JSON file onto a listview for a personal project

Background: I'm trying to parse a JSON file into a listview. I've been following the "Building the Weather app" and "listview &adapter" tutorials and trying to apply those lessons into my project

Problem: I'm at the parcelable section of the tutorial and I'm having difficultly applying it into my project. My project is different from the weather app because it only has one list and no buttons. I'm having trouble passing information from my adapter to the MainActivity and I would appreciate any advice. Thank you

github: https://github.com/jykwok/Journalpedia

json file: https://core.ac.uk/api-v2/articles/search/cells?page=1&pageSize=20&metadata=true&fulltext=false&citations=false&similar=false&duplicate=false&urls=true&faithfulMetadata=false&apiKey=PykSKLeJz7ZldYQbNWoTDvwCfnarFXOg

1 Answer

Seth Kroger
Seth Kroger
56,413 Points

You only really need to make something Parcelable if you're passing it through an Intent or Bundle. Since you only have the one Activity so far, it's not really needed. Really all you need to do is store the resulting data a member variable in MainActivity and create the Adapter passing along the list of parsed data.

Jeffrey Kwok
Jeffrey Kwok
3,006 Points

Hi Seth,

Thank you for the reply. So I will go ahead and declare member variables in MainActivity. I have already created an adapter class but how do I initialize the adapter in MainActivity?

Seth Kroger
Seth Kroger
56,413 Points

There really isn't any difference in initializing the adapter in MainActivity or another Activity. You construct a new one with the list and context and set the ListView's adatper to it. The difference is in how you come by the list data, not how you use it.