1

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

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
CuadCode
  • 21
  • 4
  • If you try to log the value if `snapshot.title`, are you getting something printed out? – Alex Mamo Sep 21 '22 at 11:32
  • Do you get the lateinit property hasn't been initialized error?? – Vivek Gupta Sep 21 '22 at 12:12
  • Replying to Vivek, I don't get any errors, the app runs normally and replying to Alex, the application doesn't print anything, the progressbar never disappears. – CuadCode Sep 21 '22 at 12:22
  • I created a log of `snapshot.title` in `onBindViewHolder` and it doesn't even show up, as if it wasn't going through. – CuadCode Sep 21 '22 at 12:39
  • The code looks llike it matches the data structure, so I'm not immediately sure what's going wrong. If you set breakpoints on the first lines inside `onCreateViewHolder` and `onBindViewHolder` and then run in the debugger, does it reach these breakpoints? – Frank van Puffelen Sep 21 '22 at 14:26
  • @FrankvanPuffelen I have tested it and indeed they never reach those breakpoints. – CuadCode Sep 21 '22 at 17:07
  • Hmm... if you set a breakpoint in `onError` too, does it get there? If not, I somewhat expect that your code is not connected to the database at all, probably because you downloaded `google-services.json` before creating the database. You'll want to download an updated version, and use that in your Android app instead. Also see: https://stackoverflow.com/questions/69197762/fail-to-write-data-to-firebase-realtime-database-android/69198094#69198094 – Frank van Puffelen Sep 21 '22 at 17:15
  • 1
    @FrankvanPuffelen In fact, I updated the services file several times but nothing, I tried the solutions in the other post you mention, but updating the file again didn't work and the second option didn't work for me or I don't know how to implement it, but the way I did it didn't work either :( – CuadCode Sep 21 '22 at 17:47
  • Hmm... I'm not sure what could be causing the problem in that case. You might want to [enable debug logging](https://firebase.google.com/docs/reference/android/com/google/firebase/database/FirebaseDatabase.html#setLogLevel(com.google.firebase.database.Logger.Level)) and check your logcat for anything suspicious. Otherwise, I hope somebody spots what might be going wrong. – Frank van Puffelen Sep 21 '22 at 20:21

1 Answers1

0

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....

CuadCode
  • 21
  • 4