Created
December 28, 2020 20:16
The custom adapter for emails list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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