Skip to content

Commit

Permalink
03_02e
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgassner committed Jul 20, 2020
1 parent ff64fef commit 7e03ea6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
11 changes: 9 additions & 2 deletions app/src/main/java/com/example/plainolnotes4/MainFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,12 +37,17 @@ 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)
})

return binding.root
}

override fun onItemClick(noteId: Int) {
Log.i("noteLogging", "onItemClick: received note id $noteId")

}

}
10 changes: 9 additions & 1 deletion app/src/main/java/com/example/plainolnotes4/NotesListAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<NoteEntity>) :
class NotesListAdapter(private val notesList: List<NoteEntity>,
private val listener: ListItemListener) :
RecyclerView.Adapter<NotesListAdapter.ViewHolder>() {

inner class ViewHolder(itemView: View) :
Expand All @@ -27,6 +28,13 @@ class NotesListAdapter(private val notesList: List<NoteEntity>) :
val note = notesList[position]
with(holder.binding) {
noteText.text = note.text
root.setOnClickListener{
listener.onItemClick(noteId =note.id)
}
}
}

interface ListItemListener {
fun onItemClick(noteId: Int)
}
}

0 comments on commit 7e03ea6

Please sign in to comment.