I can't get my RecyclerViewFirebase to work

Viewed 27

I'm trying to populate a recyclerview with the data I have in Firebase and I can't get it in any way, I've looked at several websites, videos and I see that they always do practically the same, so I don't understand why it doesn't work. I would appreciate if someone can help me, I am trying to make a recycler view that shows an image and a title with the data that is in the realtime database. This is the class I'm working on: ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ ㅤㅤㅤㅤㅤㅤㅤㅤㅤ

class HomeFragment : Fragment() {

    private lateinit var mbinding         : FragmentHomeBinding
    private lateinit var mFirebaseAdapter : FirebaseRecyclerAdapter<Snapshot, SnapshotHolder> //TODO 2 adapter
    private lateinit var mLayoutManager   : RecyclerView.LayoutManager

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        mbinding = FragmentHomeBinding.inflate(inflater, container, false)
        return mbinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val query   = FirebaseDatabase.getInstance().reference.child("snapshots") //TODO 3 adapter
        val options = FirebaseRecyclerOptions.Builder<Snapshot>().setQuery(query, Snapshot::class.java).build() //TODO 4 adapter

        mFirebaseAdapter = object : FirebaseRecyclerAdapter<Snapshot, SnapshotHolder>(options){ //TODO 5 adapter

            private lateinit var mContext : Context

            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SnapshotHolder { //TODO 6 adapter
                mContext = parent.context
                val view = LayoutInflater.from(mContext).inflate(R.layout.item_snapshot, parent, false)
                return SnapshotHolder(view)
            }

            override fun onBindViewHolder(holder: SnapshotHolder, position: Int, model: Snapshot) { //TODO 7 adapter
                val snapshot = getItem(position)

                with(holder){
                    listener(snapshot)
                    binding.tvTitle.text = snapshot.title
                    Glide.with(mContext)
                        .load(snapshot.photoUrl)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .centerCrop()
                        .into(binding.imgPhoto)
                }
            }

            override fun onDataChanged() { //TODO 8 adapter
                super.onDataChanged()
                mbinding.pbCarga.visibility = View.GONE
            }

            override fun onError(error: DatabaseError) { //TODO 9 adapter
                super.onError(error)
                Toast.makeText(mContext, error.message, Toast.LENGTH_SHORT).show()
            }
        }

        mLayoutManager = LinearLayoutManager(context) //TODO 10 adapter
        mbinding.rvHome.apply {
            setHasFixedSize(true)
            layoutManager = mLayoutManager
            adapter       = mFirebaseAdapter
        }
    }

    override fun onStart() { //TODO 11 adapter
        super.onStart()
        mFirebaseAdapter.startListening()
    }

    override fun onStop() { //TODO 12 adapter
        super.onStop()
        mFirebaseAdapter.stopListening()
    }

    inner class SnapshotHolder(view: View) : RecyclerView.ViewHolder(view){  //TODO 1 adapter
        val binding = ItemSnapshotBinding.bind(view)

        fun listener(snapshot: Snapshot){

        }
    }
    
}

This is my data class Snapshot:

@IgnoreExtraProperties
data class Snapshot(
    var id       : String               = "",
    var title    : String               = "",
    var photoUrl : String               = "",
    var likeList : Map<String, Boolean> = mutableMapOf()
)

This is my realtime Database:

enter image description here

The rules of the realtime database:

enter image description here

The rest of the code is here: https://github.com/JahelCuadrado/SnapshotsApp

1 Answers

The solution (not exactly a solution) was to do exactly the same in a new project, without changing anything at all, so I understand that the problem was some kind of cache that could not be deleted in any way.

Although the problem has been fixed and creating a new project was feasible because it was not very big, this would not be an option in a very advanced project. I wonder how to deal with this in the future if it happens again....

Related