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 trialJun Kakeno
iOS Development with Swift Techdegree Graduate 24,842 PointsWhy do we pass the adapter to Etzy.getActiveListings()?
Could anyone explain to me why we pass the adapter to getActiveListings? When getActiveListings is expecting a callback to be passed as the parameter.
//Set up recycler view
mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL));
adapter = new ListingAdapter(this);
mRecyclerView.setAdapter(adapter);
if (savedInstanceState == null){
showLoading();
Etzy.getActiveListings(adapter); <-------Here
1 Answer
Boban Talevski
24,793 PointsI wouldn't be able to explain the insides of how Retrofit does all that it does as I only just learned of Retrofit and did all these things for the first time, but the simple explanation is that our ListingAdapter class implements the Callback<ActiveListings> interface, which in Java terms, lets us use the object of type ListingAdapter as a parameter in methods which expect a type of Callback<ActiveListings>.
I mean that's why the compiler is not complaining and all that.
Now as to why we did it like that, from what I understood so far, is that our ListingAdapter is what should listen for callbacks (don't ask why, it's probably best practice :) ), which is why it implements the callback interface, requiring us to implement success and failure methods which will be called when we get a reply from the network call. And that's why the object of the type ListingAdapter is passed as a parameter to the Etsy.getActiveListings() method.
Hope that made some sense, still trying to get all this inside my head :).