Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,405 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,405 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.