Reputation: 355
As I have implemented a room library in my project , I add data to database , But Recyclerview is showing only one item .
class NotesAdapter : RecyclerView.Adapter<NotesAdapter.NotesViewHolder>() {
class NotesViewHolder(val binding: NotesCardLayoutBinding) :
RecyclerView.ViewHolder(binding.root)
private val differCallback = object : DiffUtil.ItemCallback<Notes>() {
override fun areItemsTheSame(oldItem: Notes, newItem: Notes): Boolean {
return oldItem.noteId == newItem.noteId
}
override fun areContentsTheSame(oldItem: Notes, newItem: Notes): Boolean {
return oldItem == newItem
}
}
val differ = AsyncListDiffer(this,differCallback)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NotesViewHolder {
val view =
NotesCardLayoutBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return NotesViewHolder(view)
}
override fun onBindViewHolder(holder: NotesViewHolder, position: Int) {
val notes = differ.currentList[position]
holder.binding.tvTitle.text = notes.noteTitle.toString()
holder.binding.tvDesc.text = notes.notesDesc.toString()
}
override fun getItemCount(): Int {
return differ.currentList.size
}
}
" My Notes Fragment where recylerview is intialized "
class NotesFragment : Fragment() {
private lateinit var binding: FragmentNotesBinding
private lateinit var notesViewModel: NotesViewModel
private lateinit var notesAdapter : NotesAdapter
private fun recyclerviewSetup() {
notesAdapter = NotesAdapter()
val recyclerView = binding.rvNotes
recyclerView.adapter = notesAdapter
recyclerView.setHasFixedSize(true)
recyclerView.layoutManager = LinearLayoutManager(requireContext())
activity?.let {
notesViewModel.getAllNotes.observe(viewLifecycleOwner , Observer { noteList->
notesAdapter.differ.submitList(noteList)
})
}
}
}
It may be the problem of database or Adapter please gyzz help me out github
Upvotes: 0
Views: 71
Reputation: 12953
The problem is not with fetching data, I've cloned your repo and checked, it is while inserting, do not user Int?
for primary key, just use Int
Notes.kt
@Entity(tableName = "Notes Table")
data class Notes(
@PrimaryKey(autoGenerate = true)
var noteId: Int,
@ColumnInfo(name = "Title of Notes")
val noteTitle: String?,
@ColumnInfo(name = "Description of Notes")
val notesDesc: String?
)
Also you have another problem is notes_card_layout
, you are setting height to match_parent
which is not correct, use below
notes_card_layout.xml
<?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"
android:layout_width="match_parent"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:cardElevation="5dp"
app:cardCornerRadius="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="5dp"
android:layout_marginVertical="5dp"
android:orientation="vertical">
<TextView
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:text="@string/title_card" />
<TextView
android:id="@+id/tvDesc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:text="@string/desc_card" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
replace above two code snippets and try, below is a snapshot from my modified changes
Upvotes: 1