0

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.

Ben
  • 5
  • 3

1 Answers1

0

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!

ItzDavi
  • 443
  • 4
  • 13
  • i updated the code, i'll update it on the question so you can see it but the for loop isn't working i tried it and tested it with a log to see if it runs but nothing. – Ben Jun 12 '23 at 10:36
  • issue i'm having is i don't know how to get the response from the home fragment to the notification fragment – Ben Jun 12 '23 at 11:15
  • You can pass data or objects between activites and fragment with Interfaces: try this https://stackoverflow.com/a/76371800/14120486 – ItzDavi Jun 12 '23 at 11:17