【问题标题】:kotlin nested recyclerview add datakotlin嵌套recyclerview添加数据
【发布时间】:2018-09-25 18:37:24
【问题描述】:

我们正在尝试设计一个嵌套的 recyclerview,我们得到了使用两个适配器的想法。我们不了解的是数据源的构建。我们使用 SQLite DB 作为数据源。我们的设计是描述杂货店部门的父文本字段。就像这些部门中带有子项目的农产品和酒类是西红柿鳄梨和啤酒。
如果我们使用两个模型和两个数据库表,我们如何将子项与部门关联?
我们考虑了一个具有这种格式的 DB 表记录 1 生产西红柿记录 2 空鳄梨记录 3 白酒啤酒。这似乎不太聪明。所以接下来我们考虑使用 JOINS 或 UNION 调用从我们的两个表中创建一个新表,其中一个包含 Dept 和其他 Items。
我们将如何布置这两个表,以便它们将项目与各自的部门相关联?
我们在这里也猜测我们的 ViewHolder 需要是一个自己的类,它与父适配器和子适配器对话。 我们将发布我们试图模仿的设计照片(复制)
我们的问题是如何设计数据库表?
我们是否需要一个与两个适配器接口的 ViewHolder 类?
如何创建两个表以及调用什么类型的JOIN来创建一个新表?

我们查看了这个链接,这个想法很棒,但是他的代码没有相同的数据源。一个是日期,另一个可能是 SQLite Kotlin Nested

好的,我们有一个工作数据库,两个适配器 DeptAdapter 和 ItemAdapter 工作但不是同时工作。 DEPT_TABLE 和 ITEM_TABLE 这两个表有数据
两个表的视图显示在 ListActivity 中,带有 activity_list.xml
ListActivity 不能同时提供两个表视图
我们认为错误的是 recyclerview_dept.xml 中声明的 recyclerview 没有涉及,所有工作或视图都由 id 为 rvListActivity 的 activity_list.xml 中的 recyclerview 提供

下面发布的代码带有一个问题

class ListActivity : AppCompatActivity() {

private var RecyclerAdapter: DeptAdapter? = null
private var RecyclerAdapter2:ItemAdapter? =null
private var recyclerView: RecyclerView? = null
private var recyclerView2: RecyclerView? = null
private val db = DBHelper(this)
private var deptList:List<DEPT> = ArrayList()
private var itemList:List<ITEM> = ArrayList()
private var linearLayoutManager: LinearLayoutManager? = null
private var linearLayoutManager2: LinearLayoutManager? = null

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

    initViews()

}// end onCreate

override fun onResume() {
    super.onResume()

    initDB()
}

// This is ONLY called when Activity is in onResume state
private fun initDB() {
    deptList = db.queryAllDEPT()
    //itemList = db.queryAllITEM()
    if(deptList.isEmpty()){
        title = "No Records in DB"
    }else{
        title = "Contact List"
    }
    println("########################################### onSTART")
    RecyclerAdapter = DeptAdapter(deptList = deptList, context = applicationContext)
    //RecyclerAdapter2 = ItemAdapter(itemList = itemList, context = applicationContext)
    (recyclerView as RecyclerView).adapter = RecyclerAdapter
    //(recyclerView2 as RecyclerView).adapter = RecyclerAdapter2
}

private fun initViews() {

    recyclerView = this.findViewById(R.id.rvListActivity)
    RecyclerAdapter = DeptAdapter(deptList = deptList, context = applicationContext)
    linearLayoutManager = LinearLayoutManager(applicationContext)
    (recyclerView as RecyclerView).layoutManager = linearLayoutManager!!

    //recyclerView2 = this.findViewById(R.id.rvListActivity)
    //RecyclerAdapter2 = ItemAdapter(itemList = itemList, context = applicationContext)
    //linearLayoutManager2 = LinearLayoutManager(applicationContext)
    //(recyclerView2 as RecyclerView).layoutManager = linearLayoutManager2!!
}

上述活动的 XML 文件

<android.support.constraint.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="match_parent"
tools:context=".ListActivity">

<android.support.v7.widget.RecyclerView
    android:id="@+id/rvListActivity"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

</android.support.v7.widget.RecyclerView>

带有额外回收器视图的 XML 文件

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="5dp"
    android:id="@+id/list_new_card">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/tvDEPT"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="16dp"
            android:layout_marginRight="16dp"
            android:gravity="center_vertical"
            android:text="I am tv tvDept"
            android:textColor="@color/color_Black"
            android:textSize="18sp"
            android:textStyle="bold" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rvDEPT"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

        </android.support.v7.widget.RecyclerView>

    </LinearLayout>

</android.support.v7.widget.CardView>

两个适配器

class DeptAdapter(deptList:List<DEPT>,internal var context: Context):RecyclerView.Adapter<DeptAdapter.DeptViewHolder>() {

private var deptList:List<DEPT> = ArrayList()
init{this.deptList = deptList}

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

override fun getItemCount(): Int {
    return deptList.size
}

override fun onBindViewHolder(holder: DeptViewHolder, position: Int) {
    val items = deptList[position]
    holder.item.text = items.dept
}

inner class DeptViewHolder(view: View) : RecyclerView.ViewHolder(view) {
    var item: TextView = view.findViewById(R.id.tvDEPT) as TextView
}

}

如果你愿意的话,子适配器

class ItemAdapter(itemList:List<ITEM>,var context: Context):RecyclerView.Adapter<ItemAdapter.ItemViewHolder>() {

private var itemList:List<ITEM> = ArrayList()
init{this.itemList = itemList}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {

    val view = LayoutInflater.from(context).inflate(R.layout.recyclerview_item,parent,false)
    return ItemViewHolder(view)
}

override fun getItemCount(): Int {
    return itemList.size
}

override fun onBindViewHolder(holder:ItemViewHolder, position: Int) {
    val items = itemList[position]
    holder.item.text = items.gitem
}

inner class ItemViewHolder(view: View) : RecyclerView.ViewHolder(view) {
    var item: TextView = view.findViewById(R.id.tvITEM) as TextView
}

}

表格设计是简单的 id 和 string 都在单独的模型中

我们的问题是如何在 ListActivity 中显示两个表?

【问题讨论】:

  • 你能格式化你的问题吗?
  • 第一件事:您在一个问题中提出多个问题。每个问题问 1 个问题,而不是像这样过于宽泛的问题,因为这很难帮助你。你的第一个问题是关于数据库设计的,所以从这个开始。 SQLite 和其他 SQL 类型的数据库密切相关。考虑表之间的关系。例如,您有一张桌子 (Departments)。拥有一个部门和一个ID。然后,不同的杂货店有一个外键指向特定 Department 的 id。
  • 显然可以回答你所有的问题,但是问一个问题然后解决那个问题。然后提出一个新问题,以使用 ViewHolder 模式等解决您的问题。很有可能,当您得出第一个问题的结论时,您可能已经自己解决了第二个问题。
  • @Darwind 我对所有问题表示歉意,因为一部分依赖于第一部分,我一次将所有部分放在桌子上。是的,我试图不浪费时间开发不起作用的数据库。所以感谢外键建议我们考虑过并将测试
  • @Grendel 没问题 - 其他人很难发布涵盖您整个情况的答案,因此最好同时提出一个问题并解决该问题,然后从那里继续前进.与您编写代码的方式相同 - 一次一行或一次一项功能或一次函数或方法;-)

标签: android sqlite android-recyclerview kotlin


【解决方案1】:

所以这真的是精巧的代码。一个问题我还没有将它绑定到 SQLite 数据库。
当我有时间将它连接到数据库时,我会发布更新。
你有多个 ides 正确的两个适配器是必须的
此处使用 List 中的 List,方法是将 ChildModel 添加到 ParentModel 作为List&lt;ChildModel&gt;
此代码属于Navendra Jha

您需要进行一些操作,因为他认为让子项独立于父项左右滚动会很有趣,因此在显示数据的 MainActivity 中将此行从水平更改为垂直

layoutManager = LinearLayoutManager(this@MainActivity, LinearLayout.VERTICAL, false)

Navendra Jha 在他的代码中使用了一个 ImageView,一旦我们了解了幕后发生的事情,我们就将其注释掉了。这是真的每个 Kotlin 开发人员都想要在她/他的工具箱中的超级充电的完整的优秀代码

【讨论】:

  • 你在政治上如此正确,她/他的 Javendra Jha 有一篇关于 Airbanb 工具箱中环氧树脂的精彩文章。我们对图书馆不大,但这个可能值得探索。谢谢,我会和你比赛先看看连接数据库。我问 Grosh 的人他们是怎么做的布局没有回应。为什么要泄露您的商业机密?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-31
  • 1970-01-01
  • 2021-12-05
  • 2022-01-02
  • 1970-01-01
  • 2018-06-04
  • 1970-01-01
相关资源
最近更新 更多