RecyclerView 在片段中未加载。

huangapple go评论56阅读模式
英文:

RecyclerView not loading in fragment

问题

我正在尝试创建一个RecyclerView来滚动浏览一个填充了对象的列表,但是当我进入指定的片段时,我不明白为什么它不加载适配器。以下是代码,有人可以告诉我我在做错什么吗?

这是适配器类:

package com.example.mobileappv2

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.mobileappv2.ui.dashboard.scannedCodes

class FoodMenuAdapter(private val menuItems: ArrayList<BookRecord>) : RecyclerView.Adapter<FoodMenuAdapter.ViewHolder>() {

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var ItemNum: TextView
        var ItemDescription: TextView
        var OPNum: TextView
        var LotNumber: TextView
        var PickQuantity: TextView
        var StockLocation: TextView
        var PcsMissing: TextView
        var buttonPL: Button

        init {
            ItemNum = itemView.findViewById(R.id.ItemNum)
            ItemDescription = itemView.findViewById(R.id.ItemDescription)
            OPNum = itemView.findViewById(R.id.OPNum)
            LotNumber = itemView.findViewById(R.id.LotNumber)
            PickQuantity = itemView.findViewById(R.id.PickQuantity)
            StockLocation = itemView.findViewById(R.id.StockLocation)
            PcsMissing = itemView.findViewById(R.id.PcsMissing)
            buttonPL = itemView.findViewById(R.id.buttonPL)
        }
    }

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

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val menuItem = menuItems[position]
        holder.ItemNum.text = menuItem.item
        holder.ItemDescription.text = menuItem.description
        holder.OPNum.text = menuItem.OPNum
        holder.LotNumber.text = menuItem.LotNumber
        holder.PickQuantity.text = menuItem.PickQuantity
        holder.StockLocation.text = menuItem.StockLocation
        holder.PcsMissing.text = menuItem.PcsMissing

        holder.buttonPL.setOnClickListener {
            val bookingID = menuItem.bookingId
            scannedCodes.add(bookingID.toString())
        }
    }

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

这是如何填充数组的方式:

val xmlDoc = Jsoup.parse(response, Parser.xmlParser())
val recordElements = xmlDoc.select("BOOKRECORD")
Log.d("RECORD_COUNT", recordElements.size.toString())

for (recordElement in recordElements) {
    val bookingId = recordElement.selectFirst("BOOKING_ID")?.text()
    val itemNum = recordElement.selectFirst("ITEM")?.text()
    val description = recordElement.selectFirst("DESCRIPTION")?.text()
    val opNum = recordElement.selectFirst("UOM")?.text()
    val lotNumber = recordElement.selectFirst("LOT_NUMBER")?.text()
    val pickQuantity = recordElement.selectFirst("OPENQTY")?.text()
    val stockLocation = recordElement.selectFirst("STOCKLOCATOR")?.text()
    val pcsMissing = recordElement.selectFirst("MISSING")?.text()

    Log.d("BOOKING_ID", bookingId ?: "null")
    Log.d("ITEM", itemNum ?: "null")
    Log.d("DESCRIPTION", description ?: "null")
    Log.d("UOM", opNum ?: "null")
    Log.d("LOT_NUMBER", lotNumber ?: "null")
    Log.d("OPENQTY", pickQuantity ?: "null")
    Log.d("STOCKLOCATOR", stockLocation ?: "null")
    Log.d("MISSING", pcsMissing ?: "null")

    val bookRecord = BookRecord(bookingId, itemNum, opNum, description, lotNumber, pickQuantity, stockLocation, pcsMissing)
    bookRecords.add(bookRecord)
}

Log.d("XML_RESPONSE", inputStream.toString())

requireActivity().runOnUiThread() {
    test.text = Picklist.toString()
}

这是如何在片段中调用适配器的方式:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    val test = "test"
    Log.d("Recyclerview", test)

    val recyclerView: RecyclerView = binding.recyclerView
    val menuItems = ArrayList<BookRecord>()
    val adapter = FoodMenuAdapter(menuItems)
    val layoutManager = LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)

    for (recordElement in ArrayList<BookRecord>()) {
        val bookingId = recordElement.bookingId
        val itemNum = recordElement.item
        val description = recordElement.description
        val opNum = recordElement.OPNum
        val lotNumber = recordElement.LotNumber
        val pickQuantity = recordElement.PickQuantity
        val stockLocation = recordElement.StockLocation
        val pcsMissing = recordElement.PcsMissing

        val bookRecord = BookRecord(bookingId, itemNum, opNum, description, lotNumber, pickQuantity, stockLocation, pcsMissing)
        menuItems.add(bookRecord)
    }

    recyclerView.apply {
        layoutManager.scrollToPosition(0)
        recyclerView.layoutManager = layoutManager
        recyclerView.adapter = adapter
    }
}
英文:

so I'm trying to create a recyclerview to scroll through a list filled with objects and such,
i can't understand why it's not loading the adapter when i go the the designated fragment. This is the code could someone maybe tell me what i'm doing wrong?

val bookRecords = ArrayList&lt;BookRecord&gt;()

data class BookRecord(
    val bookingId: String?,
    val item: String?,
    val OPNum: String?,
    val description: String?,
    val LotNumber: String?,
    val PickQuantity: String?,
    val StockLocation: String?,
    val PcsMissing: String?
)

This is the adapter class:

package com.example.mobileappv2

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.mobileappv2.ui.dashboard.scannedCodes

class FoodMenuAdapter(private val menuItems: ArrayList&lt;BookRecord&gt;) : RecyclerView.Adapter&lt;FoodMenuAdapter.ViewHolder&gt;() {

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        // Define properties for each view in your custom layout
        var ItemNum: TextView
        var ItemDescription: TextView
        var OPNum: TextView
        var LotNumber: TextView
        var PickQuantity: TextView
        var StockLocation: TextView
        var PcsMissing: TextView
        var buttonPL: Button

        init {
            ItemNum = itemView.findViewById(R.id.ItemNum)
            ItemDescription = itemView.findViewById(R.id.ItemDescription)
            OPNum = itemView.findViewById(R.id.OPNum)
            LotNumber = itemView.findViewById(R.id.LotNumber)
            PickQuantity = itemView.findViewById(R.id.PickQuantity)
            StockLocation = itemView.findViewById(R.id.StockLocation)
            PcsMissing = itemView.findViewById(R.id.PcsMissing)
            buttonPL = itemView.findViewById(R.id.buttonPL)
        }

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        // Inflate the custom layout for the food menu item
        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.picklistsetup, parent, false)
        return ViewHolder(itemView)

    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        val menuItem = menuItems[position]

        // Set the data from the current item to the respective views
        holder.ItemNum.text = menuItem.item
        holder.ItemDescription.text = menuItem.description
        holder.OPNum.text = menuItem.OPNum
        holder.LotNumber.text = menuItem.LotNumber
        holder.PickQuantity.text = menuItem.PickQuantity
        holder.StockLocation.text = menuItem.StockLocation
        holder.PcsMissing.text = menuItem.PcsMissing

        // Set click listener for the button
        holder.buttonPL.setOnClickListener {

            val bookingID = menuItem.bookingId
            scannedCodes.add(bookingID.toString())

        }

    }

    override fun getItemCount(): Int {

        return menuItems.size

    }
}

This is how the array is being filled out:

val xmlDoc = Jsoup.parse(response, Parser.xmlParser())
                        val recordElements = xmlDoc.select(&quot;BOOKRECORD&quot;)
                        Log.d(&quot;RECORD_COUNT&quot;, recordElements.size.toString())

                        for (recordElement in recordElements) {
                            val bookingId = recordElement.selectFirst(&quot;BOOKING_ID&quot;)?.text()
                            val itemNum = recordElement.selectFirst(&quot;ITEM&quot;)?.text()
                            val description = recordElement.selectFirst(&quot;DESCRIPTION&quot;)?.text()
                            val opNum = recordElement.selectFirst(&quot;UOM&quot;)?.text()
                            val lotNumber = recordElement.selectFirst(&quot;LOT_NUMBER&quot;)?.text()
                            val pickQuantity = recordElement.selectFirst(&quot;OPENQTY&quot;)?.text()
                            val stockLocation = recordElement.selectFirst(&quot;STOCKLOCATOR&quot;)?.text()
                            val pcsMissing = recordElement.selectFirst(&quot;MISSING&quot;)?.text()

                            // Logging the extracted values
                            Log.d(&quot;BOOKING_ID&quot;, bookingId ?: &quot;null&quot;)
                            Log.d(&quot;ITEM&quot;, itemNum ?: &quot;null&quot;)
                            Log.d(&quot;DESCRIPTION&quot;, description ?: &quot;null&quot;)
                            Log.d(&quot;UOM&quot;, opNum ?: &quot;null&quot;)
                            Log.d(&quot;LOT_NUMBER&quot;, lotNumber ?: &quot;null&quot;)
                            Log.d(&quot;OPENQTY&quot;, pickQuantity ?: &quot;null&quot;)
                            Log.d(&quot;STOCKLOCATOR&quot;, stockLocation ?: &quot;null&quot;)
                            Log.d(&quot;MISSING&quot;, pcsMissing ?: &quot;null&quot;)

                            val bookRecord = BookRecord(bookingId, itemNum, opNum, description, lotNumber, pickQuantity, stockLocation, pcsMissing)
                            bookRecords.add(bookRecord)
                        }

                        Log.d(&quot;XML_RESPONSE&quot;, inputStream.toString())

                        requireActivity().runOnUiThread() {
                            test.text = Picklist.toString()

                        }

And this is how i'm calling the adapter in the fragment:

 override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val test = &quot;test&quot;

        Log.d(&quot;Recyclerview&quot;,test)

        val recyclerView: RecyclerView = binding.recyclerView
        val menuItems = ArrayList&lt;BookRecord&gt;()
        val adapter = FoodMenuAdapter(menuItems)
        val layoutManager = LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false) // Set the layout manager

        for (recordElement in ArrayList&lt;BookRecord&gt;()) {
            val bookingId = recordElement.bookingId
            val itemNum = recordElement.item
            val description = recordElement.description
            val opNum = recordElement.OPNum
            val lotNumber = recordElement.LotNumber
            val pickQuantity = recordElement.PickQuantity
            val stockLocation = recordElement.StockLocation
            val pcsMissing = recordElement.PcsMissing

            val bookRecord = BookRecord(bookingId, itemNum, opNum, description, lotNumber, pickQuantity, stockLocation, pcsMissing)
            menuItems.add(bookRecord)

        }

        recyclerView.apply { layoutManager.scrollToPosition(0)
            recyclerView.layoutManager = layoutManager
            recyclerView.adapter = adapter }

    }

i'm just expecting the list to be loaded into the recyclerview and that's the last part of the project.

答案1

得分: 0

It seems like you are not loading data correctly. You should populate your list before adding the adapter to avoid having an empty list in your RecyclerView. Here's the corrected code snippet:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    ...

    val recyclerView: RecyclerView = binding.recyclerView
    val menuItems = ArrayList<BookRecord>()

    val xmlDoc = Jsoup.parse(response, Parser.xmlParser())
    val recordElements = xmlDoc.select("BOOKRECORD")

    for (recordElement in recordElements) {
        ...
        menuItems.add(bookingId, item, ...)
    }

    val adapter = FoodMenuAdapter(menuItems)

    ...
}

This should ensure that your list is populated before setting it in the adapter for your RecyclerView.

英文:

Seems like you are not loading data correctly.

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    ...

    val recyclerView: RecyclerView = binding.recyclerView
    --&gt; val menuItems = ArrayList&lt;BookRecord&gt;() &lt;-- You create your list but you never populate it
    --&gt; Before adding the adapter you should populate menuItems &lt;--
    val adapter = FoodMenuAdapter(menuItems)
    
    ...
}

You should populate your list in the fragment, without creating it in your parse function.

for (recordElement in recordElements) {
   ...


   --&gt; This should go before your adapter to populate your list &lt;--
   val bookRecord = BookRecord(bookingId, itemNum, opNum, description, lotNumber, pickQuantity, stockLocation, pcsMissing)
   bookRecords.add(bookRecord)
   -&gt; &lt;--
}

This for-loop should be used to populate your menuItems so you will have something similar to this in your fragment:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    ...

    val recyclerView: RecyclerView = binding.recyclerView
    val menuItems = ArrayList&lt;BookRecord&gt;()

    val xmlDoc = Jsoup.parse(response, Parser.xmlParser())
    val recordElements = xmlDoc.select(&quot;BOOKRECORD&quot;)
    --&gt; Populate your list BEFORE the adapter or it will be empty
    for (recordElement in recordElements) {
       ...
       menuItems.add(bookingId, item, ...)
    }
    
    val adapter = FoodMenuAdapter(menuItems)

    ...
}

EDIT

You should have something like this. If you try looping inside ArrayList&lt;BookRecord&gt;() you will have still nothing to loop on.

val xmlDoc = Jsoup.parse(response, Parser.xmlParser())
val recordElements = xmlDoc.select(&quot;BOOKRECORD&quot;)
for (recordElement in recordElements) {
   ...
   menuItems.add(bookingId, item, ...)
}

Please, let me know if this solved your problem. If not, feel free to update your question, happy coding!

huangapple
  • 本文由 发表于 2023年6月12日 16:20:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76454753.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定