Open lshiuber opened 8 years ago
:+1: Thanks Lin for submitting 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.
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();
}
}
Attach any listeners in the ViewHolder to cut down on memory usage. Rather than setting OnClickListener
instances in RecyclerView.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);
}
});
}
}
}
getPopularPosts
? You should have a method that represents each endpoint and then accepts specific parameters and a “handler” as outlined here.getPopularPosts
in your activity and the onSuccess should deserialize the response and then use it to populate a RecyclerView.Adapter
.onFailure
method in JsonHttpResponseHandler
, and at a minimum, notifying the user via a Toast
that the network request failed.
/cc @codepathreview @codepath