androidjsonkotlinandroid-navigation-graphpass-data

How to pass List of data from NavGraph in android kotlin Android


Getting this error:

Type mismatch: inferred type is List<BookParkingResponse.Data> but List<BookParkingResponse.Data.ParkingRequests> was expected unable to pass data bewteeen fragments

Nav Graph code:

navGraph
        <argument
            android:name="bookingRequestList"
            app:argType="com.jvm.di.model.parkingSpot.response.BookParkingResponse.Data" />

Response class

@Parcelize
data class BookParkingResponse(
    @SerializedName("data") var data: List<Data>? = null
):Parcelable {
    @Parcelize
    data class Data(
        @SerializedName("parkingRequests") var parkingRequests: List<ParkingRequests>? = null
    ) : Parcelable {
        @Parcelize
        data class ParkingRequests(
            @SerializedName("parkingSpotId") var parkingSpotId: Int? = null,
            @SerializedName("bookingStatus") var bookingStatus: String? = null,
            @SerializedName("amount") var amount: Double? = null):Parcelable
}
// pass data on itemClick 
 findNavController().navigate( MyParkFragmentDirections.toAllRequestListFragment(it))

// how I receive data in another fragments
private val args: AllRequestListFragmentArgs by navArgs()
 allRequestListAdapter = AllRequestListAdapter()
            rvItems.adapter = allRequestListAdapter
            allRequestListAdapter.allParkingRequestList = listOf(args.bookingRequestList)
// how i create list in adapter 
 internal var allParkingRequestList: List<BookParkingResponse.Data> by Delegates.observable(
        emptyList()
    ) { _, _, _ -> notifyDataSetChanged() }

Solution

  • So, I resolve my issue. corrected navgraph

    <argument
                android:name="bookingRequestList"  app:argType="di.model.parkingSpot.response.BookParkingResponse$Data[]"/>
    
    // this is how i pass the data
     findNavController().navigate(
                        MyParkFragmentDirections.toAllRequestListFragment(
                            it.parkingSpot?.parkingName!!,
                            arrayOf(it)
                        )
                    )
    

    So, this is the easy way to pass data, but what type of data is in our model class? what fragment was sent to the adapter and what type of data the adapter wants. it's a little bit tricky but when you understand. then it will become very easy for you