Home > Software design >  Why are my firebase images not being displayed in the RecyclerView?
Why are my firebase images not being displayed in the RecyclerView?

Time:01-27

JSON:

 people
     uid /// the actual uid of each user not a child named uid
       imageUrl

Below is the relevant code:

    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_homepage)
    val user = FirebaseAuth.getInstance().currentUser
    if (user != null) {
        val query = FirebaseDatabase.getInstance().reference.child("people").child("imageUrl")

        val options = FirebaseRecyclerOptions.Builder<DataSnapshot>()
            .setQuery(query, DataSnapshot::class.java)
            .build()
        Log.d("DataSnapshot1", options.toString())


        class PhotoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            val photoImageView = itemView.findViewById<ImageView>(R.id.image_view)
        }

        val adapter = object : FirebaseRecyclerAdapter<DataSnapshot, PhotoViewHolder>(options) {
            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PhotoViewHolder {
                val view =
                    LayoutInflater.from(parent.context).inflate(R.layout.item_photo, parent, false)
                return PhotoViewHolder(view)
            }

            override fun onBindViewHolder(holder: PhotoViewHolder, position: Int, model: DataSnapshot) {
                Log.d("DataSnapshot", model.toString())
                val imageUrl = model.child("imageUrl").getValue(String::class.java)
                if (imageUrl != null) {
                    Log.d("ImageUrl", imageUrl)
                    Glide.with(holder.itemView.context)
                        .load(imageUrl)
                        .into(holder.photoImageView)
                }
            }

        }

        val recyclerView = findViewById<RecyclerView>(R.id.cycle)
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = adapter
        adapter.startListening()
    }
}

What I know: The problem is not in the XMLs of this page or the image to be displayed on the recycler view. Also, ("ImageUrl") and "DataSnapshot" don't print out. But "DataSnapshot1" does print out.

CodePudding user response:

I assume you're trying to list all the items under the people node?

In that case, you need to update your query to:

val query = FirebaseDatabase.getInstance().reference.child("people")

I don't think FirebaseUI supports DataSnapshot as a model class, so I would suggest creating a separate class to hold the data. I have created a Person class as an example:

data class Person(var imageUrl: String? = null)

And then your options would become:

        val options = FirebaseRecyclerOptions.Builder<Person>()
            .setQuery(query, Person::class.java)
            .build()

And your adapter:

        val adapter = object : FirebaseRecyclerAdapter<Person, PhotoViewHolder>(options) {
            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PhotoViewHolder {
                val view =
                    LayoutInflater.from(parent.context).inflate(R.layout.item_photo, parent, false)
                return PhotoViewHolder(view)
            }

            override fun onBindViewHolder(holder: PhotoViewHolder, position: Int, model: Person) {
                val imageUrl = model.imageUrl
                if (imageUrl != null) {
                    Log.d("ImageUrl", imageUrl)
                    Glide.with(holder.itemView.context)
                        .load(imageUrl)
                        .into(holder.photoImageView)
                }
            }

        }
  • Related