I am creating a Notes taking app. Where on the main screen I have a floating action button . When the user will click on that button a screen will open to take the title and description of the note. Then the note will add on the screen on a recycler view on the main screen. Then if the recycler item is clicked again a dialog box will open to update the notes, and I have also a delete button to delete the notes. I have created the Entity table, Dao class , the database , repository and viewmodel. Also add the Recycler view and it's adapter. But I am struggling on the point where I am taking the title and the description from user and then how to set it on the view in the recycler view and how I can use the same layout for update also and where to implement the delete and update functionality
Here is the adapter code for recycler view
class NotesRecycle() : RecyclerView.Adapter<NotesRecycle.NotesViewHolder>() {
val allNotes=ArrayList<NoteTable>()
inner class NotesViewHolder(itemView:View):RecyclerView.ViewHolder(itemView)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NotesViewHolder {
val viewholder=NotesViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.notes_item,parent,false))
return viewholder
}
override fun onBindViewHolder(holder: NotesViewHolder, position: Int) {
val currentNotes=allNotes[position]
holder.itemView.tvtitle.text=currentNotes.title
holder.itemView.tvdescription.text=currentNotes.description
}
override fun getItemCount(): Int {
return allNotes.size
}
}
here is my MainActivity code
class MainActivity : AppCompatActivity() {
lateinit var viewModel: NotesViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
recyclerview.layoutManager=LinearLayoutManager(this)
val adapter=NotesRecycle()
recyclerview.adapter=adapter
val dao=NotesDataBase.getDataBase(applicationContext).getNoteDao()
val repository=NotesRepository(dao)
viewModel=ViewModelProvider(this,NoteViewModeFactory(repository)).get(NotesViewModel::class.java)
btnadd.setOnClickListener{
val diolog=Dialog(this)
diolog.setContentView(R.layout.add_update_lay_item)
diolog.btnADD.setOnClickListener {
val title=diolog.etTitle.text.toString()
val description=diolog.etDescription.text.toString()
viewModel.insertNote(NoteTable(0,title,description))
diolog.dismiss()
}
diolog.show()
}
}
}
The repository class
class NotesRepository (private val noteDao: NoteDao) {
val allNotes:LiveData<List<NoteTable>> = noteDao.getAllNotes()
suspend fun insert(noteTable: NoteTable){
noteDao.insert(noteTable)
}
suspend fun update(noteTable: NoteTable){
noteDao.update(noteTable)
}
suspend fun delete(noteTable: NoteTable){
noteDao.delete(noteTable)
}
}
and the view model
class NotesViewModel(private val notesRepository: NotesRepository): ViewModel() {
val getNotes:LiveData<List<NoteTable>> = notesRepository.allNotes
fun insertNote(noteTable: NoteTable){
viewModelScope.launch(Dispatchers.IO) {notesRepository.insert(noteTable) }
}
fun updateNote(noteTable: NoteTable){
viewModelScope.launch(Dispatchers.IO) {
notesRepository.update(noteTable)
}
}
fun deleteNote(noteTable: NoteTable){
viewModelScope.launch(Dispatchers.IO) { notesRepository.delete(noteTable) }
}
}
the layout for add the notes and update
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:background="@color/teal_200">
<TextView
android:id="@+id/tvText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="ADD NOTES"
android:textSize="30dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textColor="@color/white"
android:textStyle="bold"/>
<EditText
android:id="@+id/etTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="ADD TITLE"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvText"
android:layout_marginTop="10dp"
android:background="#fff"
android:inputType="text"/>
<EditText
android:id="@+id/etDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/etTitle"
android:hint="ADD DESCRIPTION"
android:layout_marginTop="10dp"
android:background="#fff"
android:inputType="text"/>
<Button
android:id="@+id/btnADD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD"
android:textColor="@color/white"
app:layout_constraintTop_toBottomOf="@+id/etDescription"
app:layout_constraintEnd_toEndOf="parent"
android:backgroundTint="@color/black"/>
</androidx.constraintlayout.widget.ConstraintLayout>
please take a little time and help me to solve this