源码

android – kotlin recyclerview数据没有显示


我们的问题是如何显示两个SQLite数据库表中的父数据和子数据?

我们有两个表添加到两个ArrayLists childList和parentList.

以下是每个模型类的

class ModelParent {
    var idD:Int = 0
    var dept:String = ""
    var fkD:Int = 0
    var children: List = mutableListOf()
    //var children: ArrayList? = null
    //var children: List  by Delegates.notNull()
    constructor (children: List) : 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)
}

我们将发布查看活动

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

    private var parentList:List = ArrayList()
    private var childList:List = 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([email protected], LinearLayout.VERTICAL, false)
            adapter = ViewAdapter(parentList)
            adapter = ViewChildAdapter(children = childList)
        }
    }
}

ViewActivity具有此XML文件




    

两个适配器和相应的XML文件

class ViewAdapter(private val parents:List):RecyclerView.Adapter() {
    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):RecyclerView.Adapter() {
    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文件



    

        

        
    




    

仅加载ViewActivity时,才会显示childList.

我们已尝试了各种更改,但无法显示父列表,但使用了GET fun显示了parentList数据,因此我们知道它在列表中.

我们可以运行乐趣theGET并创建一个似乎徒劳的新ArrayList.

我们担心的是显示parentList,然后在显示childList时将其删除.

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

那么我们的问题是如何在View Activity中以有组织的方式显示父子数据?

我们正在根据@Cruces答案添加新代码
此代码的一些问题无法理解
1.我们无法运行有趣的连接来创建newList
2.只有包含Class的接收者才能调用Parent和Child ViewHolder
3.内部类太多了,我们需要一个外部嵌套注释吗?
4.或者如果父和子实现接口,则列表< 我们不知道如何编写接口并将其连接到JoinAdapter

虽然答案提出了新的问题,但我们觉得最好在这种情况下提出问题=背景幽默
这是JoinAdapter的FIX

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

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

我将把View Activity调用发布到Join Adapter
代码没有FAIL它只显示什么?

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

这是ViewJoinActivity,它将加载父数据无子数据

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

0

从活动中调用Join Adapter是个问题吗? ?
此Activity有一个XML相关文件,其中包含RecyclerView rv_parent

(0)

本文由 投稿者 创作,文章地址:https://blog.isoyu.com/archives/android-kotlin-recyclerviewshujumeiyouxianshi.html
采用知识共享署名4.0 国际许可协议进行许可。除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为:9月 25, 2019 at 11:12 下午

热评文章