Skip to content

Instantly share code, notes, and snippets.

@magdamiu
Created December 28, 2020 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magdamiu/bfd83e3ab22db927370800dbc90c2bb5 to your computer and use it in GitHub Desktop.
Save magdamiu/bfd83e3ab22db927370800dbc90c2bb5 to your computer and use it in GitHub Desktop.
The custom adapter for emails list
public class EmailAdapter extends RecyclerView.Adapter<EmailViewHolder> {
private List<Email> emails;
private Context context;
public EmailAdapter(Context context, List<Email> emails) {
this.emails = emails;
this.context = context;
}
// creates the items and add them to the RecyclerView, just the layout
@NonNull
@Override
public EmailViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(context).inflate(R.layout.email_item, parent, false);
return new EmailViewHolder(itemView);
}
// binds (displays) the content from the list of emails for each item
@Override
public void onBindViewHolder(@NonNull EmailViewHolder holder, int position) {
Email currentEmail = emails.get(position);
holder.getTextViewFrom().setText(currentEmail.getFromName());
holder.getTextViewSubject().setText(currentEmail.getSubject());
holder.getTextViewBody().setText(currentEmail.getShortBody());
}
// we tell to the Recycler View how many items to display
@Override
public int getItemCount() {
return emails.size();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment