首页 > 解决方案 > 我只能从 Room 数据库中将一件物品返回给 recyclerview

问题描述

由于我在项目中实现了一个房间库,我将数据添加到数据库中,但 Recyclerview 只显示一个项目。

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
}

}

“ 初始化 recylerview 的我的笔记片段”


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)
            })
        }
    }

}

可能是数据库或适配器的问题,请gyzz帮我解决一下 github

标签: androidkotlinmvvmfragment

解决方案


问题不在于获取数据,我已经克隆了你的 repo 并检查了,它是在插入时,不要用户Int?主键,只需使用Int

笔记.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?
)

您还有另一个问题是notes_card_layout,您设置的高度match_parent不正确,请在下面使用

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>

替换上面的两个代码片段并尝试,下面是我修改后的更改的快照

在此处输入图像描述


推荐阅读