Class MoodHistoryAdapter

java.lang.Object
androidx.recyclerview.widget.RecyclerView.Adapter<MoodHistoryAdapter.MoodViewHolder>
com.kernelcrew.moodapp.ui.MoodHistoryAdapter

public class MoodHistoryAdapter extends androidx.recyclerview.widget.RecyclerView.Adapter<MoodHistoryAdapter.MoodViewHolder>
Adapter class for binding mood event data to the RecyclerView in the mood history screen.

This adapter manages the display of mood events in a list format. It shows the date and sequential numbering of each mood event. It also handles click events on items through the MoodHistoryAdapter.OnItemClickListener interface.

  • Constructor Details

    • MoodHistoryAdapter

      public MoodHistoryAdapter(List<MoodEvent> moods, MoodHistoryAdapter.OnItemClickListener listener)
      Constructs a new MoodHistoryAdapter with the specified data and listener.
      Parameters:
      moods - List of mood events to display; if null, an empty list is used
      listener - Callback for item click events
  • Method Details

    • setMoods

      public void setMoods(List<MoodEvent> moods)
      Updates the adapter's data set with a new list of mood events.

      Replaces the existing list of mood events and notifies the adapter to refresh the RecyclerView.

      Parameters:
      moods - New list of mood events to display
    • onCreateViewHolder

      @NonNull public MoodHistoryAdapter.MoodViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
      Creates a new ViewHolder when needed by the RecyclerView.

      Inflates the mood item layout and wraps it in a ViewHolder.

      Specified by:
      onCreateViewHolder in class androidx.recyclerview.widget.RecyclerView.Adapter<MoodHistoryAdapter.MoodViewHolder>
      Parameters:
      parent - The ViewGroup into which the new View will be added
      viewType - The view type of the new View
      Returns:
      A new ViewHolder that holds the View for each mood item
    • onBindViewHolder

      public void onBindViewHolder(@NonNull MoodHistoryAdapter.MoodViewHolder holder, int position)
      Binds data to the ViewHolder at the specified position.

      Sets the date and event number text, and configures the click listener for the item view.

      Specified by:
      onBindViewHolder in class androidx.recyclerview.widget.RecyclerView.Adapter<MoodHistoryAdapter.MoodViewHolder>
      Parameters:
      holder - The ViewHolder to bind data to
      position - The position of the item in the data set
    • getItemCount

      public int getItemCount()
      Returns the total number of items in the data set.
      Specified by:
      getItemCount in class androidx.recyclerview.widget.RecyclerView.Adapter<MoodHistoryAdapter.MoodViewHolder>
      Returns:
      The number of mood events in the list
    • getItems

      public List<MoodEvent> getItems()
      Returns a copy of the current list of mood events.

      Creates a new ArrayList to avoid exposing the internal list to modification.

      Returns:
      A copy of the current list of mood events