【问题标题】:kotlin recyclerview display sqlite parent and child table datakotlin recyclerview 显示sqlite父子表数据
【发布时间】:2025-11-29 16:50:02
【问题描述】:

我们的问题是关于数据库设计或如何在 recyclerview 小部件中查看父表和子表?

这是我们想要的视图

生产
西红柿
鳄梨


啤酒

Department 表模型看起来像这样 IT IS THE PARENT

class ViewDeptModel(
    var idD:Int = 0
    var dept:String = ""
)

Item 表模型是相同的 IT IS THE CHILD

class ViewItemModel(
    var idI:Int = 0
    var item:String = ""
)

所以在recyclerview中查看数据我们考虑过使用这个模型

data class ParentModel(
    val title : String = "",
    val children : List<ViewItemModel>
)

所以首先显示 Dept,然后显示相应的子级
我们如何知道与 Parent(dept) 关联的子级何时完成?
所以下一个 Parent 将与它的孩子一起显示

如果我们给 Child 表一个外键来引用 Parent id,这会阻止使用 JOINS 和 UNIONS

所以在下面这个链接中查看 For Next Loop 就像 4 岁的 Java 代码
LINK

我们有点迷失在哪里放置这些代码。在显示数据的 ViewActivity 中。我们是否将 In action collect data 代码添加到 ViewActivity? 看起来它是它自己的类
我们假设 For Next 循环进入 ViewActivity。 这段代码的工作原理是我们真正迷失的地方!

@model IEnumerable<ProductUserViewModel>

foreach(var item in Model)
{
   <li>@item.User.Name</li>
   foreach(var product in item.Products)
   {
     <li>@product.Name</li>
   }
}

如果我们需要放弃第三个模型和 For Next Loops 的想法,我们很乐意倾听

【问题讨论】:

  • @MikeT 我们可以在您有时间的时候使用您的专业知识
  • 为什么 FK 会阻止 JOINS 和 UNION?我认为您的问题需要更多信息。

标签: android sqlite android-recyclerview kotlin


【解决方案1】:

假设所有数据都可以按如下形式从数据库中加载:

部门:

data class Department(val id: Int, val title: String, val children: ArrayList<Item>)

项目:

data class Item(val id: Int, val title: String)

可以计算部门在列表中的位置。例如,第一个部门位于索引 0,第二个部门位于 departments.get(0).children.size + 1,依此类推。

完整代码:

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val data = arrayListOf<Department>(
            Department(0, "Department1",
                    arrayListOf(
                            Item(0, "Item1.1"),
                            Item(1, "Item1.2"),
                            Item(3, "Item1.3")
                    )),
            Department(0, "Department2",
                    arrayListOf(
                            Item(0, "Item2.1")
                    )),
            Department(0, "Department3",
                    arrayListOf(
                            Item(0, "Item3.1"),
                            Item(1, "Item3.2")
                    )),
            Department(0, "Department4",
                    arrayListOf(
                            Item(0, "Item4.1"),
                            Item(1, "Item4.2"),
                            Item(1, "Item4.3")
                    ))
    )

    recycler_view.layoutManager = LinearLayoutManager(this)
    recycler_view.adapter = Adapter(this, data)

}
}

class Adapter(val context: Context, val list: ArrayList<Department>): RecyclerView.Adapter<VH>() {

var total: Int = 0
var sectionIndices: ArrayList<Int> = arrayListOf()
var sectionSizes: ArrayList<Int> = arrayListOf()

init{
    var index = 0
    var pos = 0
    for (d in list){
        sectionIndices.add(pos)
        sectionSizes.add(d.children.size)
        pos += (1 + d.children.size)
        index += 1
    }
    total = pos
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
    val view = LayoutInflater.from(context).inflate(android.R.layout.simple_list_item_1, parent, false)
    return VH(view)
}

override fun getItemCount(): Int {
    return total
}

override fun onBindViewHolder(holder: VH, position: Int) {
    for (section in 0 .. sectionIndices.size - 1){
        if (position >= sectionIndices.get(section)
                && position <= sectionIndices.get(section) + sectionSizes.get(section)){
            val i = position - sectionIndices.get(section)
            when (i){
                0 -> {
                    // department
                    val department = list.get(section)
                    holder.textView.typeface = Typeface.DEFAULT_BOLD
                    holder.textView.text = department.title
                }
                else -> {
                    // item
                    val item = list.get(section).children.get(i - 1)
                    holder.textView.typeface = Typeface.DEFAULT
                    holder.textView.text = item.title
                }
            }
            break
        }
    }
}
}

class VH(val view: View): RecyclerView.ViewHolder(view){

val textView: TextView
init {
    textView = view.findViewById(android.R.id.text1)
}

}

data class Department(val id: Int, val title: String, val children: ArrayList<Item>)

data class Item(val id: Int, val title: String)

【讨论】:

  • 我了解部门基于大小的增量我想我可能没有解释的是我们不知道项目表中有多少项目所以如果 Dept 0 有 3 个孩子和 Dept,请使用您的代码1 有 5 个孩子,您的代码如何知道将孩子 3 放在 1 部门,这里是代码 medium.com/@navendra/… 的链接,将尝试测试您的代码
  • 那么请澄清您的问题:您有来自medium.com/@navendra/… 的普通 RecyclerView 还是嵌套 RecyclerView?您是否已经有从数据库中加载部门/项目的代码,或者目标是编写它?
  • 我们可以用数据创建两个表,这已经完成嵌套的recyclerview还没有写但是我们计划按照medium.com的代码只需用SQLite数据替换数据供应我们可以查看这两个表我们称之为后端或只是一种创建表的方法问题是链接嵌套回收器视图的表或显示数据抱歉迟到的回复
  • 如果从数据库加载数据是主要目标,那么可以使用 SQLiteDatabase.query() (developer.android.com/reference/android/database/sqlite/…) 来完成。我创建了一个示例github.com/atarasenko/nestedRecycler
  • 数据库和应用程序的 GitHub 代码在我尝试使用时出现太多错误将看看我是否可以 FIX 自己的错误可能在您这边运行 感谢您的坚持