Skip to content

Commit

Permalink
Step.02-Solution-Display-Data
Browse files Browse the repository at this point in the history
  • Loading branch information
Jill Heske authored and yenerm committed Jun 5, 2019
1 parent 483b656 commit ef0fb43
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,21 @@

package com.example.android.trackmysleepquality.sleeptracker

import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.android.trackmysleepquality.R
import com.example.android.trackmysleepquality.TextItemViewHolder
import com.example.android.trackmysleepquality.database.SleepNight

class SleepNightAdapter: RecyclerView.Adapter<TextItemViewHolder>() {
class SleepNightAdapter : RecyclerView.Adapter<TextItemViewHolder>() {

// TODO (03) Add a custom setter to data that calls notifyDataSetChanged().
var data = listOf<SleepNight>()
var data = listOf<SleepNight>()
set(value) {
field = value
notifyDataSetChanged()
}

override fun getItemCount() = data.size

Expand All @@ -35,10 +41,10 @@ class SleepNightAdapter: RecyclerView.Adapter<TextItemViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TextItemViewHolder {

// TODO (01) Inflate the text_item_view layout.
val layoutInflater = LayoutInflater.from(parent.context)
val view = layoutInflater
.inflate(R.layout.text_item_view, parent, false) as TextView

// TODO (02) Return the inflated view.

TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
return TextItemViewHolder(view)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,14 @@ class SleepTrackerFragment : Fragment() {
}
})

// TODO (04) Create a new SleepNightAdapter variable,
// and bind it to the RecyclerView’s Adapter.
val adapter = SleepNightAdapter()
binding.sleepList.adapter = adapter

// TODO (05) Create an observer on sleepTrackerViewModel.nights that tells
// the Adapter when there is new data.
sleepTrackerViewModel.nights.observe(viewLifecycleOwner, Observer {
it?.let {
adapter.data = it
}
})

return binding.root
}
Expand Down

0 comments on commit ef0fb43

Please sign in to comment.