-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotesAdapter.kt
57 lines (44 loc) · 2.07 KB
/
NotesAdapter.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.example.myapplicationnewtaskmanagement
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.Toast
class NotesAdapter(private var notes: List<Note>, context: Context) : RecyclerView.Adapter<NotesAdapter.NoteViewHolder>() {
private val db:NoteDataBaseHelper = NoteDataBaseHelper(context)
class NoteViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val titleTextView: TextView = itemView.findViewById(R.id.titleTextView)
val contentTextView: TextView = itemView.findViewById(R.id.contentTextView)
val updateButton: ImageView = itemView.findViewById(R.id.updateButton)
val deleteButton: ImageView = itemView.findViewById(R.id.deleteButton)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NoteViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.note_item, parent, false)
return NoteViewHolder(view)
}
override fun getItemCount(): Int = notes.size
override fun onBindViewHolder(holder: NoteViewHolder, position: Int) {
val note = notes[position]
holder.titleTextView.text = note.title
holder.contentTextView.text = note.content
holder.updateButton.setOnClickListener {
val intent = Intent(holder.itemView.context, UpdateNoteActivity::class.java).apply {
putExtra( "note_id", note.id)
}
holder.itemView.context.startActivity(intent)
}
holder.deleteButton.setOnClickListener{
db.deleteNote(note.id)
refreshData(db.getAllNotes())
Toast.makeText(holder.itemView.context,"Note Deleted", Toast.LENGTH_SHORT).show()
}
}
fun refreshData(newNotes: List<Note>){
notes = newNotes
notifyDataSetChanged()
}
}