From 7e03ea6fb008dcfde9ca3586d774648139907a3d Mon Sep 17 00:00:00 2001 From: David Gassner Date: Mon, 20 Jul 2020 12:37:24 -0700 Subject: [PATCH] 03_02e --- .../java/com/example/plainolnotes4/MainFragment.kt | 11 +++++++++-- .../com/example/plainolnotes4/NotesListAdapter.kt | 10 +++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/example/plainolnotes4/MainFragment.kt b/app/src/main/java/com/example/plainolnotes4/MainFragment.kt index 15eb1c1..65a01f8 100644 --- a/app/src/main/java/com/example/plainolnotes4/MainFragment.kt +++ b/app/src/main/java/com/example/plainolnotes4/MainFragment.kt @@ -11,8 +11,10 @@ import androidx.lifecycle.ViewModelProvider import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.LinearLayoutManager import com.example.plainolnotes4.databinding.MainFragmentBinding +import kotlin.math.log -class MainFragment : Fragment() { +class MainFragment : Fragment(), + NotesListAdapter.ListItemListener{ private lateinit var viewModel: MainViewModel private lateinit var binding: MainFragmentBinding @@ -35,7 +37,7 @@ class MainFragment : Fragment() { viewModel.notesList.observe(viewLifecycleOwner, Observer { Log.i("noteLogging", it.toString()) - adapter = NotesListAdapter(it) + adapter = NotesListAdapter(it, this@MainFragment) binding.recyclerView.adapter = adapter binding.recyclerView.layoutManager = LinearLayoutManager(activity) }) @@ -43,4 +45,9 @@ class MainFragment : Fragment() { return binding.root } + override fun onItemClick(noteId: Int) { + Log.i("noteLogging", "onItemClick: received note id $noteId") + + } + } \ No newline at end of file diff --git a/app/src/main/java/com/example/plainolnotes4/NotesListAdapter.kt b/app/src/main/java/com/example/plainolnotes4/NotesListAdapter.kt index 1dcea3a..a9386e6 100644 --- a/app/src/main/java/com/example/plainolnotes4/NotesListAdapter.kt +++ b/app/src/main/java/com/example/plainolnotes4/NotesListAdapter.kt @@ -7,7 +7,8 @@ import androidx.recyclerview.widget.RecyclerView import com.example.plainolnotes4.data.NoteEntity import com.example.plainolnotes4.databinding.ListItemBinding -class NotesListAdapter(private val notesList: List) : +class NotesListAdapter(private val notesList: List, + private val listener: ListItemListener) : RecyclerView.Adapter() { inner class ViewHolder(itemView: View) : @@ -27,6 +28,13 @@ class NotesListAdapter(private val notesList: List) : val note = notesList[position] with(holder.binding) { noteText.text = note.text + root.setOnClickListener{ + listener.onItemClick(noteId =note.id) + } } } + + interface ListItemListener { + fun onItemClick(noteId: Int) + } } \ No newline at end of file