My RecyclerView is displaying empty CardView. I don't see where I went wrong. Android Studio doesn't fail at compile tile and the app doesn't fail during run time either. Any help will be appreciated.
This is my data class
data class Notes(
var title: String,
var description: String
)
This is my adapter
class MyNotesAdapter(private val notesList: MutableList<Notes>) : RecyclerView.Adapter<MyNotesAdapter.ViewHolder>() {
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
var textViewTitle: TextView = view.findViewById(R.id.textViewTitle)
var textViewDescr: TextView = view.findViewById(R.id.textViewDescr)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
var view = LayoutInflater.from(parent.context).inflate(R.layout.my_notes_adapter_layout,
parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.textViewTitle.text = notesList[position].title
holder.textViewDescr.text = notesList[position].description
}
override fun getItemCount(): Int = notesList.size
}
This is my adapter layout
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="16dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp">
<TextView
android:id="@+id/textViewTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TITLE HERE"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textColor="@color/black"
app:layout_constraintTop_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="@+id/textViewDescr"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="DESCR HERE"
android:textColor="@color/black"
app:layout_constraintTop_toBottomOf="@id/textViewTitle"
app:layout_constraintStart_toStartOf="@id/textViewTitle"
app:layout_constraintEnd_toEndOf="@id/textViewTitle" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
That is how I setup my RecyclerView in my main activity
private fun setupRecyclerView() {
val myNotesAdapter: MyNotesAdapter = MyNotesAdapter(notesList)
val linearLayoutManager: LinearLayoutManager = LinearLayoutManager(this)
linearLayoutManager.orientation = RecyclerView.VERTICAL
recyclerViewNotes = findViewById(R.id.recyclerViewNotes)
recyclerViewNotes.layoutManager = linearLayoutManager
recyclerViewNotes.adapter = myNotesAdapter
}
My RecyclerView function gets called here
private fun setupDialogBox() {
var view = LayoutInflater.from(this).inflate(R.layout.add_notes_dialog_layout, null)
var dialog = AlertDialog.Builder(this)
.setView(view)
.setCancelable(false)
.create()
dialog.show()
buttonSubmit = view.findViewById(R.id.buttonSubmit)
editTextTitle = view.findViewById(R.id.editTextTitle)
editTextDescr = view.findViewById(R.id.editTextDescr)
buttonSubmit.setOnClickListener {
notes = Notes(editTextTitle.text.toString(), editTextDescr.text.toString())
notesList.add(notes)
setupRecyclerView()
dialog.hide()
}
}
My dialog gets called within main activity onCreate()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_my_notes)
......
fabButton.setOnClickListener {
setupDialogBox()
}
}