RecyclerView 在片段中未加载。

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

RecyclerView not loading in fragment

问题

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

这是适配器类:

  1. package com.example.mobileappv2
  2. import android.view.LayoutInflater
  3. import android.view.View
  4. import android.view.ViewGroup
  5. import android.widget.Button
  6. import android.widget.TextView
  7. import androidx.recyclerview.widget.RecyclerView
  8. import com.example.mobileappv2.ui.dashboard.scannedCodes
  9. class FoodMenuAdapter(private val menuItems: ArrayList<BookRecord>) : RecyclerView.Adapter<FoodMenuAdapter.ViewHolder>() {
  10. inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
  11. var ItemNum: TextView
  12. var ItemDescription: TextView
  13. var OPNum: TextView
  14. var LotNumber: TextView
  15. var PickQuantity: TextView
  16. var StockLocation: TextView
  17. var PcsMissing: TextView
  18. var buttonPL: Button
  19. init {
  20. ItemNum = itemView.findViewById(R.id.ItemNum)
  21. ItemDescription = itemView.findViewById(R.id.ItemDescription)
  22. OPNum = itemView.findViewById(R.id.OPNum)
  23. LotNumber = itemView.findViewById(R.id.LotNumber)
  24. PickQuantity = itemView.findViewById(R.id.PickQuantity)
  25. StockLocation = itemView.findViewById(R.id.StockLocation)
  26. PcsMissing = itemView.findViewById(R.id.PcsMissing)
  27. buttonPL = itemView.findViewById(R.id.buttonPL)
  28. }
  29. }
  30. override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
  31. val itemView = LayoutInflater.from(parent.context).inflate(R.layout.picklistsetup, parent, false)
  32. return ViewHolder(itemView)
  33. }
  34. override fun onBindViewHolder(holder: ViewHolder, position: Int) {
  35. val menuItem = menuItems[position]
  36. holder.ItemNum.text = menuItem.item
  37. holder.ItemDescription.text = menuItem.description
  38. holder.OPNum.text = menuItem.OPNum
  39. holder.LotNumber.text = menuItem.LotNumber
  40. holder.PickQuantity.text = menuItem.PickQuantity
  41. holder.StockLocation.text = menuItem.StockLocation
  42. holder.PcsMissing.text = menuItem.PcsMissing
  43. holder.buttonPL.setOnClickListener {
  44. val bookingID = menuItem.bookingId
  45. scannedCodes.add(bookingID.toString())
  46. }
  47. }
  48. override fun getItemCount(): Int {
  49. return menuItems.size
  50. }
  51. }

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

  1. val xmlDoc = Jsoup.parse(response, Parser.xmlParser())
  2. val recordElements = xmlDoc.select("BOOKRECORD")
  3. Log.d("RECORD_COUNT", recordElements.size.toString())
  4. for (recordElement in recordElements) {
  5. val bookingId = recordElement.selectFirst("BOOKING_ID")?.text()
  6. val itemNum = recordElement.selectFirst("ITEM")?.text()
  7. val description = recordElement.selectFirst("DESCRIPTION")?.text()
  8. val opNum = recordElement.selectFirst("UOM")?.text()
  9. val lotNumber = recordElement.selectFirst("LOT_NUMBER")?.text()
  10. val pickQuantity = recordElement.selectFirst("OPENQTY")?.text()
  11. val stockLocation = recordElement.selectFirst("STOCKLOCATOR")?.text()
  12. val pcsMissing = recordElement.selectFirst("MISSING")?.text()
  13. Log.d("BOOKING_ID", bookingId ?: "null")
  14. Log.d("ITEM", itemNum ?: "null")
  15. Log.d("DESCRIPTION", description ?: "null")
  16. Log.d("UOM", opNum ?: "null")
  17. Log.d("LOT_NUMBER", lotNumber ?: "null")
  18. Log.d("OPENQTY", pickQuantity ?: "null")
  19. Log.d("STOCKLOCATOR", stockLocation ?: "null")
  20. Log.d("MISSING", pcsMissing ?: "null")
  21. val bookRecord = BookRecord(bookingId, itemNum, opNum, description, lotNumber, pickQuantity, stockLocation, pcsMissing)
  22. bookRecords.add(bookRecord)
  23. }
  24. Log.d("XML_RESPONSE", inputStream.toString())
  25. requireActivity().runOnUiThread() {
  26. test.text = Picklist.toString()
  27. }

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

  1. override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
  2. super.onViewCreated(view, savedInstanceState)
  3. val test = "test"
  4. Log.d("Recyclerview", test)
  5. val recyclerView: RecyclerView = binding.recyclerView
  6. val menuItems = ArrayList<BookRecord>()
  7. val adapter = FoodMenuAdapter(menuItems)
  8. val layoutManager = LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
  9. for (recordElement in ArrayList<BookRecord>()) {
  10. val bookingId = recordElement.bookingId
  11. val itemNum = recordElement.item
  12. val description = recordElement.description
  13. val opNum = recordElement.OPNum
  14. val lotNumber = recordElement.LotNumber
  15. val pickQuantity = recordElement.PickQuantity
  16. val stockLocation = recordElement.StockLocation
  17. val pcsMissing = recordElement.PcsMissing
  18. val bookRecord = BookRecord(bookingId, itemNum, opNum, description, lotNumber, pickQuantity, stockLocation, pcsMissing)
  19. menuItems.add(bookRecord)
  20. }
  21. recyclerView.apply {
  22. layoutManager.scrollToPosition(0)
  23. recyclerView.layoutManager = layoutManager
  24. recyclerView.adapter = adapter
  25. }
  26. }
英文:

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?

  1. val bookRecords = ArrayList&lt;BookRecord&gt;()
  2. data class BookRecord(
  3. val bookingId: String?,
  4. val item: String?,
  5. val OPNum: String?,
  6. val description: String?,
  7. val LotNumber: String?,
  8. val PickQuantity: String?,
  9. val StockLocation: String?,
  10. val PcsMissing: String?
  11. )

This is the adapter class:

  1. package com.example.mobileappv2
  2. import android.view.LayoutInflater
  3. import android.view.View
  4. import android.view.ViewGroup
  5. import android.widget.Button
  6. import android.widget.TextView
  7. import androidx.recyclerview.widget.RecyclerView
  8. import com.example.mobileappv2.ui.dashboard.scannedCodes
  9. class FoodMenuAdapter(private val menuItems: ArrayList&lt;BookRecord&gt;) : RecyclerView.Adapter&lt;FoodMenuAdapter.ViewHolder&gt;() {
  10. inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
  11. // Define properties for each view in your custom layout
  12. var ItemNum: TextView
  13. var ItemDescription: TextView
  14. var OPNum: TextView
  15. var LotNumber: TextView
  16. var PickQuantity: TextView
  17. var StockLocation: TextView
  18. var PcsMissing: TextView
  19. var buttonPL: Button
  20. init {
  21. ItemNum = itemView.findViewById(R.id.ItemNum)
  22. ItemDescription = itemView.findViewById(R.id.ItemDescription)
  23. OPNum = itemView.findViewById(R.id.OPNum)
  24. LotNumber = itemView.findViewById(R.id.LotNumber)
  25. PickQuantity = itemView.findViewById(R.id.PickQuantity)
  26. StockLocation = itemView.findViewById(R.id.StockLocation)
  27. PcsMissing = itemView.findViewById(R.id.PcsMissing)
  28. buttonPL = itemView.findViewById(R.id.buttonPL)
  29. }
  30. }
  31. override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
  32. // Inflate the custom layout for the food menu item
  33. val itemView = LayoutInflater.from(parent.context).inflate(R.layout.picklistsetup, parent, false)
  34. return ViewHolder(itemView)
  35. }
  36. override fun onBindViewHolder(holder: ViewHolder, position: Int) {
  37. val menuItem = menuItems[position]
  38. // Set the data from the current item to the respective views
  39. holder.ItemNum.text = menuItem.item
  40. holder.ItemDescription.text = menuItem.description
  41. holder.OPNum.text = menuItem.OPNum
  42. holder.LotNumber.text = menuItem.LotNumber
  43. holder.PickQuantity.text = menuItem.PickQuantity
  44. holder.StockLocation.text = menuItem.StockLocation
  45. holder.PcsMissing.text = menuItem.PcsMissing
  46. // Set click listener for the button
  47. holder.buttonPL.setOnClickListener {
  48. val bookingID = menuItem.bookingId
  49. scannedCodes.add(bookingID.toString())
  50. }
  51. }
  52. override fun getItemCount(): Int {
  53. return menuItems.size
  54. }
  55. }

This is how the array is being filled out:

  1. val xmlDoc = Jsoup.parse(response, Parser.xmlParser())
  2. val recordElements = xmlDoc.select(&quot;BOOKRECORD&quot;)
  3. Log.d(&quot;RECORD_COUNT&quot;, recordElements.size.toString())
  4. for (recordElement in recordElements) {
  5. val bookingId = recordElement.selectFirst(&quot;BOOKING_ID&quot;)?.text()
  6. val itemNum = recordElement.selectFirst(&quot;ITEM&quot;)?.text()
  7. val description = recordElement.selectFirst(&quot;DESCRIPTION&quot;)?.text()
  8. val opNum = recordElement.selectFirst(&quot;UOM&quot;)?.text()
  9. val lotNumber = recordElement.selectFirst(&quot;LOT_NUMBER&quot;)?.text()
  10. val pickQuantity = recordElement.selectFirst(&quot;OPENQTY&quot;)?.text()
  11. val stockLocation = recordElement.selectFirst(&quot;STOCKLOCATOR&quot;)?.text()
  12. val pcsMissing = recordElement.selectFirst(&quot;MISSING&quot;)?.text()
  13. // Logging the extracted values
  14. Log.d(&quot;BOOKING_ID&quot;, bookingId ?: &quot;null&quot;)
  15. Log.d(&quot;ITEM&quot;, itemNum ?: &quot;null&quot;)
  16. Log.d(&quot;DESCRIPTION&quot;, description ?: &quot;null&quot;)
  17. Log.d(&quot;UOM&quot;, opNum ?: &quot;null&quot;)
  18. Log.d(&quot;LOT_NUMBER&quot;, lotNumber ?: &quot;null&quot;)
  19. Log.d(&quot;OPENQTY&quot;, pickQuantity ?: &quot;null&quot;)
  20. Log.d(&quot;STOCKLOCATOR&quot;, stockLocation ?: &quot;null&quot;)
  21. Log.d(&quot;MISSING&quot;, pcsMissing ?: &quot;null&quot;)
  22. val bookRecord = BookRecord(bookingId, itemNum, opNum, description, lotNumber, pickQuantity, stockLocation, pcsMissing)
  23. bookRecords.add(bookRecord)
  24. }
  25. Log.d(&quot;XML_RESPONSE&quot;, inputStream.toString())
  26. requireActivity().runOnUiThread() {
  27. test.text = Picklist.toString()
  28. }

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

  1. override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
  2. super.onViewCreated(view, savedInstanceState)
  3. val test = &quot;test&quot;
  4. Log.d(&quot;Recyclerview&quot;,test)
  5. val recyclerView: RecyclerView = binding.recyclerView
  6. val menuItems = ArrayList&lt;BookRecord&gt;()
  7. val adapter = FoodMenuAdapter(menuItems)
  8. val layoutManager = LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false) // Set the layout manager
  9. for (recordElement in ArrayList&lt;BookRecord&gt;()) {
  10. val bookingId = recordElement.bookingId
  11. val itemNum = recordElement.item
  12. val description = recordElement.description
  13. val opNum = recordElement.OPNum
  14. val lotNumber = recordElement.LotNumber
  15. val pickQuantity = recordElement.PickQuantity
  16. val stockLocation = recordElement.StockLocation
  17. val pcsMissing = recordElement.PcsMissing
  18. val bookRecord = BookRecord(bookingId, itemNum, opNum, description, lotNumber, pickQuantity, stockLocation, pcsMissing)
  19. menuItems.add(bookRecord)
  20. }
  21. recyclerView.apply { layoutManager.scrollToPosition(0)
  22. recyclerView.layoutManager = layoutManager
  23. recyclerView.adapter = adapter }
  24. }

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:

  1. override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
  2. super.onViewCreated(view, savedInstanceState)
  3. ...
  4. val recyclerView: RecyclerView = binding.recyclerView
  5. val menuItems = ArrayList<BookRecord>()
  6. val xmlDoc = Jsoup.parse(response, Parser.xmlParser())
  7. val recordElements = xmlDoc.select("BOOKRECORD")
  8. for (recordElement in recordElements) {
  9. ...
  10. menuItems.add(bookingId, item, ...)
  11. }
  12. val adapter = FoodMenuAdapter(menuItems)
  13. ...
  14. }

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.

  1. override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
  2. super.onViewCreated(view, savedInstanceState)
  3. ...
  4. val recyclerView: RecyclerView = binding.recyclerView
  5. --&gt; val menuItems = ArrayList&lt;BookRecord&gt;() &lt;-- You create your list but you never populate it
  6. --&gt; Before adding the adapter you should populate menuItems &lt;--
  7. val adapter = FoodMenuAdapter(menuItems)
  8. ...
  9. }

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

  1. for (recordElement in recordElements) {
  2. ...
  3. --&gt; This should go before your adapter to populate your list &lt;--
  4. val bookRecord = BookRecord(bookingId, itemNum, opNum, description, lotNumber, pickQuantity, stockLocation, pcsMissing)
  5. bookRecords.add(bookRecord)
  6. -&gt; &lt;--
  7. }

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

  1. override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
  2. super.onViewCreated(view, savedInstanceState)
  3. ...
  4. val recyclerView: RecyclerView = binding.recyclerView
  5. val menuItems = ArrayList&lt;BookRecord&gt;()
  6. val xmlDoc = Jsoup.parse(response, Parser.xmlParser())
  7. val recordElements = xmlDoc.select(&quot;BOOKRECORD&quot;)
  8. --&gt; Populate your list BEFORE the adapter or it will be empty
  9. for (recordElement in recordElements) {
  10. ...
  11. menuItems.add(bookingId, item, ...)
  12. }
  13. val adapter = FoodMenuAdapter(menuItems)
  14. ...
  15. }

EDIT

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

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

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:

确定