英文:
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<BookRecord>()
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<BookRecord>) : RecyclerView.Adapter<FoodMenuAdapter.ViewHolder>() {
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("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()
// Logging the extracted values
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()
}
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 = "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) // Set the layout manager
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 }
}
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
--> val menuItems = ArrayList<BookRecord>() <-- You create your list but you never populate it
--> Before adding the adapter you should populate menuItems <--
val adapter = FoodMenuAdapter(menuItems)
...
}
You should populate your list in the fragment, without creating it in your parse function.
for (recordElement in recordElements) {
...
--> This should go before your adapter to populate your list <--
val bookRecord = BookRecord(bookingId, itemNum, opNum, description, lotNumber, pickQuantity, stockLocation, pcsMissing)
bookRecords.add(bookRecord)
-> <--
}
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<BookRecord>()
val xmlDoc = Jsoup.parse(response, Parser.xmlParser())
val recordElements = xmlDoc.select("BOOKRECORD")
--> 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<BookRecord>()
you will have still nothing to loop on.
val xmlDoc = Jsoup.parse(response, Parser.xmlParser())
val recordElements = xmlDoc.select("BOOKRECORD")
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!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论