0

I need to get details from the Firebase Realtime Database to RecyclerView which is in a fragment. I refer to many tutorials and finally code this. I got this error:

Error

Here's my fragment code.

class busFragment : Fragment() {

    private lateinit var dbref: DatabaseReference
    private lateinit var routeRecyclerView: RecyclerView
    private lateinit var routesArrayList: ArrayList<BusRoutes>
    

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_bus, container, false)
    }

    @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        routeRecyclerView = view.findViewById(R.id.bus_routes)
        routeRecyclerView.layoutManager = LinearLayoutManager(activity)
        routeRecyclerView.setHasFixedSize(true)

        routesArrayList = arrayListOf<BusRoutes>()
        getRouteDetails()

    }

    private fun getRouteDetails() {
        dbref = FirebaseDatabase.getInstance().getReference("BusRoutes")

        dbref.addValueEventListener(object : ValueEventListener{
            override fun onDataChange(snapshot: DataSnapshot) {
                if (snapshot.exists()){
                    for (routeSnapshot in snapshot.children){
                        val route = routeSnapshot.getValue(BusRoutes::class.java)
                        routesArrayList.add(route!!)
                    }
                    routeRecyclerView.adapter = Adapter(routesArrayList)
                }
            }

            override fun onCancelled(error: DatabaseError) {
                TODO("Not yet implemented")
            }


        })
    }

    companion object {

    }

}

here's my adapter.kt

class Adapter(private val routeslist: ArrayList<BusRoutes>) : RecyclerView.Adapter<Adapter.MyViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {

        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.items_busroutes,
            parent,false)
        return MyViewHolder(itemView as ViewGroup)

    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val currentItem = routeslist[position]

        holder.route.text = currentItem.routeNo
        holder.start.text = currentItem.start
        holder.end.text = currentItem.end
    }

    override fun getItemCount(): Int {
        return routeslist.size
    }


    class MyViewHolder(itemView:ViewGroup) : RecyclerView.ViewHolder(itemView){
        val route : TextView = itemView.findViewById(R.id.routeNo)
        val start : TextView = itemView.findViewById(R.id.startPlace)
        val end : TextView = itemView.findViewById(R.id.endPlace)

    }

}

Here's my data class.data class

Here's my firebase details.Firebase details

Here's the line 52 val route = routeSnapshot.getValue(BusRoutes::class.java)

I try to fix this many times but still cannot find it. Help me. I'm still Learning Kotlin.

updated Database

BusRoutes class:

data class BusRoutes(val routeNo: String? = null, val start: String? = null, val end: String? = null)

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
ABI
  • 85
  • 5
  • It is not clear how you saved `routeNo` in your firebase, if it is a `Long` number without quotes around as the error says so, then you need to make sure they are saved as `String` otherwise; change your data class and make `routeNo` Long – Jagar Sep 29 '21 at 08:55
  • Please show the content of your `BusRoutes` class and a clear image of your database. Please respond with @AlexMamo – Alex Mamo Sep 29 '21 at 08:59
  • @Jagar I think that's the error but why it says no adapter attached – ABI Sep 29 '21 at 09:08
  • https://stackoverflow.com/q/29141729/6296561 – Zoe Sep 29 '21 at 09:09
  • Please also add the content of your `BusRoutes` class. – Alex Mamo Sep 29 '21 at 09:13

1 Answers1

1

You are getting the following error:

failed to convert value of type java.lang.long to string

Because the routeNo field in your database is of type number, while in your class is of type String and this is not correct. Both types must match because there is no way in Kotlin in which you can convert an object type Long to String, hence that error.

The simplest solution would to change the declaration fo your BusRoutes class like so:

data class BusRoutes(val routeNo: Long? = null, val start: String? = null, val end: String? = null)
//                                 ^^
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193