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.