[Android Bootcamp] Project 1 - Day 2 - Instagram - ready for review
koulmomo opened this issue · comments
My app is complete, please review. /cc @codepathreview @codepath
👍 Nice work Mo with the second day of the assignment! We've captured some of the best practices for today's assignment below. We recommend you take a look through these point-by-point to determine how you might be able to improve your submission.
- Did you create helper methods for Activity initialization in onCreate? This keeps lifecycle methods clean and increases readability, especially as your Activity increase in complexity.
- Loading adapter content. It's preferred that a convenience method be created on the adapter to reload content. This helps DRY up code and encapsulate adapter behavior if the Activity needs to call this several times in different places.
public class CommentsAdapter extends RecyclerView.Adapter<CommentsAdapter.ViewHolder> {
List<Comment> comments;
public void refreshComments(List<Comment> newComments) {
comments = newComments;
notifyDataSetChanged();
}
}
- In RecyclerView.Adapter subclasses, context can be retrieved from either a view holder or the parent view. See this guide on Accessing context from RecyclerView.Adapter
- Attach any listeners in the ViewHolder to cut down on memory usage. Rather than setting
OnClickListener
instances inRecyclerView.Adapter.onBindViewHolder()
, which causes more objects to be created, do the following:
public class InstagramPostsAdapter extends RecyclerView.Adapter<InstagramPostsAdapter.ViewHolder> {
@Override
public void onBindViewHolder(ViewHolder holder, int i) {
InstagramPost post = posts.get(i);
holder.post = post;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private InstagramPost post;
private Button btnDoSomethingWithPost;
public ViewHolder(View itemView) {
super(itemView);
btnDoSomethingWithPost = (Button) itemView.findViewById(R.id.btnDoSomethingWithPost);
setupListeners();
}
void setupListeners() {
btnDoSomethingWithPost.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// context can be referenced via v.getContext()
doSomethingWithPost(post);
}
});
}
}
}
- Fetching data from the Instagram API to populate RecyclerViews
- Did you setup your InstagramClient with an endpoint such as
getPopularPosts
? You should have a method that represents each endpoint and then accepts specific parameters and a “handler” as outlined here. - You should then make a call to
getPopularPosts
in your activity and the onSuccess should deserialize the response and then use it to populate aRecyclerView.Adapter
. - Handling Network Failures
- When making network requests, it's important to handle error cases and take appropriate actions when the network request fails. For this reason, it is advisable to override the
onFailure
method inJsonHttpResponseHandler
, and at a minimum, notifying the user via aToast
that the network request failed.