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:
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 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.
BusRoutes class:
data class BusRoutes(val routeNo: String? = null, val start: String? = null, val end: String? = null)
CodePudding user response:
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)
// ^^