科特林recyclerview数据未示出科特林recyclerview数据未示出(kotlin re

2019-05-12 14:00发布

我们的问题是如何从两个的SQLite数据库表显示父母与子女的数据?

我们有添加两个的ArrayList两个表childListparentList

这里是模型类的每个

class ModelParent {
    var idD:Int = 0
    var dept:String = ""
    var fkD:Int = 0
    var children: List<ModelChild> = mutableListOf()
    //var children: ArrayList<ModelChild>? = null
    //var children: List<ModelChild>  by Delegates.notNull()
    constructor (children: List<ModelParent>) : this()
    companion object {
       var globalVar = 1
   }
}

class ModelChild {
    var idI:Int = 0
    var item:String = ""
    var fkI:Int = 0
}

我们有两个适配器为每个表,并会张贴代码
我们可以通过两个迭代ArrayList与此代码,并显示格式,我们想在显示数据ViewActivity

fun theGET(){
    val db = DBHelper(this)
    childList = db.queryITEM()
    parentList = db.queryDEPT()
    var PL = parentList.size

    do {
        var DEPT: String = parentList[z].dept
        var PARENT_LIST_FK = parentList.get(z).fkD
            println("========== Dept " + DEPT + " fkD " + PARENT_LIST_FK)
        val FK = PARENT_LIST_FK
        childList = db.queryALL(FK)
        var CL = childList.size
        for (a in 0..CL - 1) {
            var CHILD_ITEM = childList[a].item
            var CHILD_LIST_FK = childList[a].fkI
            println("========== item " + CHILD_ITEM+" fkI "+CHILD_LIST_FK)
        }
        z++
    }
    while (z <= PL-1)
}

我们将张贴View Activity

class ViewActivity : AppCompatActivity() {
    lateinit var recyclerView: RecyclerView

    private var parentList:List<ModelParent> = ArrayList()
    private var childList:List<ModelChild> = ArrayList()

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

        initRecycler()

    }// end onCreate

    private fun initRecycler() {
        val db = DBHelper(this)
        childList = db.queryITEM()
        parentList = db.queryDEPT()

        recyclerView = rv_parent

        recyclerView.apply{
            layoutManager = LinearLayoutManager(this@ViewActivity, LinearLayout.VERTICAL, false)
            adapter = ViewAdapter(parentList)
            adapter = ViewChildAdapter(children = childList)
        }
    }
}

ViewActivity有这个XML文件

<?xml version="1.0" encoding="utf-8"?>
<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=".ViewActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_parent"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</android.support.constraint.ConstraintLayout>

两个适配器和coresponding XML文件

class ViewAdapter(private val parents:List<ModelParent>):RecyclerView.Adapter<ViewAdapter.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.the_view,parent,false)
        return ViewHolder(view)
    }

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

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val parent = parents[position]
        holder.textView.text = parent.dept
        holder.recyclerView.apply {
            layoutManager = LinearLayoutManager(holder.recyclerView.context, LinearLayout.VERTICAL, false) as RecyclerView.LayoutManager?
            adapter = ViewChildAdapter(parent.children!!)
        }
    }

    inner class ViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView){
        val recyclerView : RecyclerView = itemView.rv_child
        val textView: TextView = itemView.textView
    }
}
class ViewChildAdapter(private val children:List<ModelChild>):RecyclerView.Adapter<ViewChildAdapter.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.child_recycler,parent,false)
        return ViewHolder(view)
    }
    override fun getItemCount(): Int {
        return children.size
    }
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val child = children[position]
        holder.textView.text = child.item
    }

    inner class ViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView){
        val textView : TextView = itemView.child_textView
    }
}

充气XML文件

<android.support.v7.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_margin="2dp"
    card_view:cardBackgroundColor="#fff"
    card_view:cardCornerRadius="5dp"
    card_view:cardElevation="4dp"
    card_view:cardUseCompatPadding="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textView"
            style="@style/Base.TextAppearance.AppCompat.Subhead"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignStart="@+id/rv_child"
            android:layout_alignParentTop="true"
            android:padding="20dp"
            android:background="@color/color_super_lightGray"
            android:text="Dept Header"
            android:textColor="@color/color_Purple"
            android:textSize="24sp"
            android:textStyle="bold" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv_child"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_alignParentBottom="true"
            android:layout_marginTop="70dp"
            android:layout_marginBottom="0dp"
            android:orientation="horizontal"
            android:paddingLeft="4dp"
            android:paddingTop="8dp"
            tools:layout_editor_absoluteX="74dp" />
    </RelativeLayout>
</android.support.v7.widget.CardView>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/child_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="32dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:background="@color/color_Transparent"
        android:padding="10dp"
        android:text="TextView"
        android:textColor="@color/color_Black"
        android:textSize="20sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

ViewActivity只加载显示的childList。

我们尝试了各种变化,不能显示的parent List ,虽然使用theGET乐趣parentList显示的数据,所以我们知道它是在列表中。

我们可以运行的乐趣theGET和包装箱一个新ArrayList ,这似乎是徒劳的。

我们关注的是, parentList显示,然后取出时childList显示。

我们不知道如何证明这一点。

所以,我们的问题是如何显示在一个有组织的方式亲子数据View Activity

我们根据@Cruces回答增加新代码
与此代码有些问题超出了理解
1.我们有没有办法运行凑热闹创建newList
2.父母与孩子ViewHolder只能用含有类的接收器可称为
3.太多的内部类的?我们需要一个外部嵌套注释?
4. or if both parent and child implement an interface a List< IItem > )
我们不知道如何编写接口,并将其连接到JoinAdapter

虽然答案带来了新的问题,我们觉得它更好地要求与在此背景下=语境幽默下面是JoinAdapter的FIX

class JoinAdapter(internal var context: Context, val parents: List<ModelParent>) : RecyclerView.Adapter<JoinAdapter.MyViewHolder>() {
val items = mutableListOf<Any>()

init {
    parents //parents should be passed as a constructor argument
            .forEach {
                items.add(it)
                items.addAll(it.children)
            }
}

override fun getItemCount(): Int = items.size;


fun getItem(position: Int): Any = items[position]

override fun getItemViewType(position: Int): Int = if (getItem(position) is ModelParent) 0 else 1

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
    var view: View? = null
    if (viewType == 0) {
        view = LayoutInflater.from(parent.context).inflate(R.layout.the_view, parent, false)
        return ParentViewHolder(view!!)
    } else {
        view = LayoutInflater.from(parent.context).inflate(R.layout.child_recycler, parent, false)
        return ChildViewHolder(view!!)
    }
}

override fun onBindViewHolder(holder: MyViewHolder, position: Int) = holder.bindData(position, getItem(position))

inner abstract class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
    abstract fun bindData(position: Int, item: Any)

}

inner class ParentViewHolder(view: View) : MyViewHolder(view) {
    override fun bindData(position: Int, item: Any) {
        val parent = item as? ModelParent ?: return
        parent.dept
        parent.fkD
        parent.children
        //bind the data here
    }

    init {
        val textView: TextView = view.textView
        var editCLICK: RelativeLayout = view.findViewById(R.id.editCLICK) as RelativeLayout
        //do the view setup here
    }

}

inner class ChildViewHolder(view: View) : MyViewHolder(view) {
    init {
        val textView : TextView = itemView.child_textView
        //do the view setup here
    }

    override fun bindData(position: Int, item: Any) {
        val child = item as? ModelChild ?: return
        child.item
        child.idI
        //bind the data here
    }
}

我要发布查看活动的号召,加入适配器
代码不会失败,它只是表明什么?

        RecyclerAdapter1 = JoinAdapter(parents = ArrayList(),context = applicationContext)
    (recyclerView as RecyclerView).adapter = RecyclerAdapter1

这里是ViewJoinActivity它会加载父数据NO子数据

class ViewJoinActivity : AppCompatActivity() {

lateinit var recyclerView: RecyclerView
private var RecyclerAdapter: JoinAdapter? = null
private var linearLayoutManager: LinearLayoutManager? = null
private val db = DBHelper(this)
private var parentList:List<ModelParent> = ArrayList()
private var childList:List<ModelChild> = ArrayList()

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

}// end onCreate

override fun onResume() {
    super.onResume()
    initDB()
}

private fun initDB() {
    parentList = db.queryDEPT()
    //childList = db.queryCHILD(1)
    childList = db.queryITEM()
    // queryCHILD only selects records with a fkI equal to idD
    // SEE THE ModelChild and ModelParent
    if(parentList.isEmpty()){
        title = "No Records in DB"
    }else{
        title = "Parent List"
    }

        RecyclerAdapter = JoinAdapter(parents = parentList, context = applicationContext)
        (recyclerView as RecyclerView).adapter = RecyclerAdapter
    }

private fun initRecycler() {
    val db = DBHelper(this)
    childList = db.queryITEM()
    parentList = db.queryDEPT()

    //recyclerView = rv_parent

    /*var PL = parentList.size
    newList.clear()
    do {
        var DEPT: String = parentList[z].dept
        var ND:String = DEPT
        var PARENT_LIST_FK = parentList.get(z).fkD
        var PL_ST = ND+" "+PARENT_LIST_FK
        newList.add(PL_ST)

        println("========== Dept " + DEPT + " fkD " + PARENT_LIST_FK)
        val FK = PARENT_LIST_FK

        childList = db.queryCHILD(FK)
        var CL = childList.size

        for (a in 0..CL - 1) {
            var CHILD_ITEM = childList[a].item
            var NI:String = childList[a].item
            var CHILD_LIST_FK = childList[a].fkI
            var IL_ST = NI+" "+CHILD_LIST_FK
            newList.add(IL_ST)
            println("========== item " + CHILD_ITEM+" fkI "+CHILD_LIST_FK)
        }
        z++

        g++
    }
    while (z <= PL-1)


    var ui = newList.size
    g=0
    for(g in 0..ui-1){
        var N2 = newList[g]

        if(N2.toString().contains("1")){
            println("********************** We Found "+N2)

        }
        println("############### BOTH = "+N2)

    }*/

    recyclerView = this.findViewById(R.id.rv_parent)
    RecyclerAdapter = JoinAdapter(parents = parentList, context = applicationContext)
    linearLayoutManager = LinearLayoutManager(applicationContext)
    (recyclerView as RecyclerView).layoutManager = linearLayoutManager!!

    //recyclerView.apply {
        //layoutManager = LinearLayoutManager(this@ViewJoinActivity, LinearLayout.VERTICAL, false)
        //adapter = JoinAdapter(children = childList)
    //}

}

}

从活动调用加入适配器是问题? ? 这一活动已经有RecyclerView rv_parent一个XML相关的文件

Answer 1:

我会做的方式是压平列表edit: like this in the constructor

val items : MutableList<Any> = mutableListOf<Any>()    

init() {
   parents //parents should be passed as a constructor argument
         .asSequence() //this is only needed if you want to also order them
         .sortedBy { it.idD } //again only if you want to sort them
         .forEach { 
              items.add(it)
              items.addAll(it.children)
          }
}

我想创建一个List <任意>(或者如果双方父母和孩子实现一个接口一个List <的iItem>),将包含所有的数据,你想看到它,所以例如,它可能看起来像这样:

val items : List<Any> = listOf(parent1, child11,child12,child13,parent2,child12,child13,child14.....etc)

然后我会实现与多个视图类型这样的单个适配器:

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

fun getItem(position: Int) : Any { //or the interface 
    return items[position]
}

override getItemViewType (position: Int) : Int {
    if (getItem(position) is ModelParent)
        return 0
    return 1
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    var view : View? = null
    if (viewType == 0) {
        view = LayoutInflater.from(parent.context).inflate(R.layout.parent_layout,parent,false)
        return ParentViewHolder(view);
    } else {
       view = LayoutInflater.from(parent.context).inflate(R.layout.child_layout,parent,false)
        return ChildViewHolder(view);
    }
}

然后我会使用两个视图持有者来表示数据如何示出了用于特定项目

 inner class ParentViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView){ ...}
 inner class ChildViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView){ ...}

之后,我可以通过获取视图,类似这样的东西的类型进行不同的绑定:

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    if (getItemType(position) == 0) {
        (holder as ParentViewHolder).bindData(....)
    } else {
        (holder as ChildViewHolder).bindData(....)
    }
}

编辑:下面是完整的适配器我建,它是基于两个布局文件:

list_item_child:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_margin="2dp"
    card_view:cardBackgroundColor="#fff"
    card_view:cardCornerRadius="5dp"
    card_view:cardElevation="4dp"
    card_view:cardUseCompatPadding="true">

    <TextView
        android:id="@+id/child_item"
        style="@style/Base.TextAppearance.AppCompat.Display3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:padding="20dp"
        android:textSize="24sp"
        android:textStyle="bold"
        tools:text="Dept Header" />

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

list_item_parent:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_margin="2dp"
    card_view:cardBackgroundColor="#fff"
    card_view:cardCornerRadius="5dp"
    card_view:cardElevation="4dp"
    card_view:cardUseCompatPadding="true">

    <TextView
        android:id="@+id/parent_department"
        style="@style/Base.TextAppearance.AppCompat.Headline"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:padding="20dp"
        tools:text="Dept Header"
        android:textSize="24sp"
        android:textStyle="bold" />

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

并且适配器会是这个样子:

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView

data class ModelParent(val id: Int, val children: List<ModelChild>)
data class ModelChild(val id: Int)


class JoinAdapter(internal var context: Context, val parents: List<ModelParent>) : RecyclerView.Adapter<JoinAdapter.MyViewHolder>() {
    val items = mutableListOf<Any>()

    init {
        parents //parents should be passed as a constructor argument
                .forEach {
                    items.add(it)
                    items.addAll(it.children)
                }
    }

    override fun getItemCount(): Int = items.size;


    fun getItem(position: Int): Any = items[position]

    override fun getItemViewType(position: Int): Int = if (getItem(position) is ModelParent) 0 else 1

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        var view: View? = null
        if (viewType == 0) {
            view = LayoutInflater.from(parent.context).inflate(R.layout.rv_list_item_parent, parent, false)
            return ParentViewHolder(view!!)
        } else {
            view = LayoutInflater.from(parent.context).inflate(R.layout.rv_list_item_child, parent, false)
            return ChildViewHolder(view!!)
        }
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) = holder.bindData(position, getItem(position))

    inner abstract class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        abstract fun bindData(position: Int, item: Any)

    }

    inner class ParentViewHolder(view: View) : MyViewHolder(view) {
        var parentDept: TextView = view.findViewById(R.id.parent_department) as TextView

        override fun bindData(position: Int, item: Any) {
            val parent = item as? ModelParent ?: return
            parentDept.text = parent.dept
        }
    }

    inner class ChildViewHolder(view: View) : MyViewHolder(view) {
        var childItem: TextView = view.findViewById(R.id.child_item) as TextView

        override fun bindData(position: Int, item: Any) {
            val child = item as? ModelChild ?: return
            childItem.text = child.item
        }
    }

}

这在单一recyclerview使用将显示其母公司以下的儿童在列表中时,



Answer 2:

这里是一个答案,关于这个程序的设计的一些观察
回答不放在同一个回收站视图列表都父子数据
但是我的回答可以帮助解决这个问题有了进一步的探索!
我们所做的是创建父列表中,并点击相应的子项中的父部门时,将显示。 这可能是一个更实用的设计,而购物你只需要看看农产品项目,而在这一节。 这意味着,通过家长和孩子的单身主列表滚动少。

建立这两个当一个关于大家都在猜测一个杂货店可能有20个以上部门(父母),所以你打算如何知道哪些项目(子数据)连接到相应的部门(父)设计两句话需要大幅度的重新设计名单。

如果你从GitHub下载的代码,你会对此有设计缺陷较好的把握

这里是所有的XML文件中的代码

主要活动导航查看活动

    fun onViewAll(view: View){
    val intent = Intent(this,ViewActivity::class.java)
    intent.putExtra("pickADAPTER",2)
    startActivity(intent)
}

这里是观活动

class ViewActivity : AppCompatActivity() {

lateinit var recyclerView: RecyclerView

private var RecyclerAdapter1: ViewAdapter? = null
private var RecyclerAdapter2: ViewChildAdapter? = null
private var linearLayoutManager: LinearLayoutManager? = null

private val db = DBHelper(this)

private var parentList:List<ModelParent> = ArrayList()
private var childList:List<ModelChild> = ArrayList()

var idD = 0
var whichADAPTER = 0

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_view)
    val bundle: Bundle = intent.extras
    idD = bundle.getInt("BIGi", 0)
    whichADAPTER = bundle.getInt("pickADAPTER",0)

    initRecycler()

}// end onCreate

override fun onResume() {
    super.onResume()
    initDB()
}

private fun initDB() {
    parentList = db.queryDEPT()
    childList = db.queryCHILD(idD)
    // queryCHILD only selects records with a fkI equal to idD
    // SEE THE ModelChild and ModelParent
    if(parentList.isEmpty()){
        title = "No Records in DB"
    }else{
        title = "Parent List"
    }

    if(whichADAPTER == 2) {
        RecyclerAdapter1 = ViewAdapter(parents = parentList, context = applicationContext)
        (recyclerView as RecyclerView).adapter = RecyclerAdapter1
    }else{
        RecyclerAdapter2 = ViewChildAdapter(children = childList)
        (recyclerView as RecyclerView).adapter = RecyclerAdapter2
    }
}

private fun initRecycler() {
    val db = DBHelper(this)
    childList = db.queryITEM()
    parentList = db.queryDEPT()

    recyclerView = rv_parent

    recyclerView = this.findViewById(R.id.rv_parent)
    RecyclerAdapter1 = ViewAdapter(parents = parentList, context = applicationContext)
    linearLayoutManager = LinearLayoutManager(applicationContext)
    (recyclerView as RecyclerView).layoutManager = linearLayoutManager!!

}

这里查看活动的XML文件

<android.support.v7.widget.RecyclerView
android:id="@+id/rv_parent"
android:layout_width="match_parent"
android:layout_height="match_parent" />

这里是查看适配器

class ViewAdapter(private val parents: List<ModelParent>, internal var context: Context):RecyclerView.Adapter<ViewAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    val view = LayoutInflater.from(parent.context).inflate(R.layout.the_view,parent,false)
    return ViewHolder(view)
}

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

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    val parent = parents[position]
    holder.textView.text = parent.dept

    holder.editCLICK.setOnClickListener {
        val i = Intent(context, ViewActivity::class.java)
        i.putExtra("BIGi",parent.idD)
        i.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        context.startActivity(i)
    }
}

inner class ViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView){
    //val recyclerView : RecyclerView = itemView.rv_child
    val textView: TextView = itemView.textView
    var editCLICK: RelativeLayout = itemView.findViewById(R.id.editCLICK) as 
    RelativeLayout

而充气XML

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="2dp"
card_view:cardBackgroundColor="#FF0000"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="4dp"
card_view:cardUseCompatPadding="true">

<RelativeLayout
    android:id="@+id/editCLICK"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView"
        style="@style/Base.TextAppearance.AppCompat.Subhead"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/rv_child"
        android:layout_alignParentTop="true"
        android:padding="10dp"
        android:background="@color/color_lightGray"
        android:text="Dept Header"
        android:textColor="@color/color_Purple"
        android:textSize="24sp"
        android:textStyle="bold" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_child"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="50dp"
        android:layout_marginBottom="0dp"
        android:orientation="horizontal"
        android:paddingLeft="4dp"
        android:paddingTop="6dp"
        tools:layout_editor_absoluteX="74dp" />

</RelativeLayout>

这是DBHelper的搜索程序

    fun queryCHILD(fkI: Int): List<ModelChild> {
    val db = this.writableDatabase
    val childList = ArrayList<ModelChild>()
    val selectQuery = "SELECT  * FROM $CHILD_TABLE WHERE $colCFK = ?"
    val cursor = db.rawQuery(selectQuery, arrayOf(fkI.toString()))
    if (cursor != null) {
        if (cursor.moveToFirst()) {
            do {
                val contact = ModelChild()
                contact.idI = Integer.parseInt(cursor.getString(cursor.getColumnIndex(colidI)))
                contact.item = cursor.getString(cursor.getColumnIndex(colItem))
                contact.fkI = Integer.parseInt(cursor.getString(cursor.getColumnIndex(colCFK)))
                childList.add(contact)
            } while (cursor.moveToNext())
        }
    }
    cursor.close()
    return childList
}

有一两件事要记下我们附OnClickListener到相对布局

 holder.editCLICK.setOnClickListener

为什么我们不知道,你可以得到parent.idD从被设置为监听器RecyclerView类型的信息?
我们需要探索这一点,但在我们的测试代码失败时,我们的努力。



Answer 3:

ViewActivity类,你首先设置recyclerView适配器ViewAdapter再到ViewChildAdapter因此,现在的适配器recyclerViewViewChildAdapter而不是ViewAdapter 。 删除此行,问题会得到解决。



文章来源: kotlin recyclerview data not showing