skip to Main Content

I want to call a function where drawableprofile is binding as a profile icon. I want to call it when "imageUrl" child doesn’t exist.

val uid = firebaseAuth.currentUser?.uid

if (uid != null) {
    database.child(uid).get().addOnSuccessListener {
        if (it.exists()) {
            val fetchimg = it.child("imageUrl").value
            Glide.with(this).load(fetchimg).into(binding.profileIcon)
        } else if {
            database.child(uid).child("imageUrl")
            val drawableprofile = resources.getDrawable(R.drawable.ic_person)
           
            Glide.with(this).load(drawableprofile).into(binding.profileIcon)
        }
    }
}

I can’t find any .isNull function etc. Thanks for help

2

Answers


  1. According to your last comment:

    I need to do a placeholder until a user chooses his own profile image.

    Since you’re using Glide for Android to display your images, then you can simply call Glide#placeholder() like this:

    Glide
        .with(this)
        .load(fetchimg) //👈
        .centerCrop()
        .placeholder(R.drawable.ic_person)
        .into(binding.profileIcon);
    
    Login or Signup to reply.
  2. Replace your code with the following:

    val uid = firebaseAuth.currentUser?.uid
    if (uid != null) {
        val valueEventListener = object : ValueEventListener {
            override fun onDataChange(snapshot: DataSnapshot) {
                if (snapshot.exists()) {
                    val imageUrl = snapshot.getValue<String>()
                    if (imageUrl != null) {
                        Glide.with(this).load(imageUrl).placeholder(R.drawable.ic_person).into(binding.profileIcon)
                        return
                    }
                    Glide.with(this).load(R.drawable.ic_profile).into(binding.profileIcon)
                }
    
                override fun onCancelled(error: DatabaseError) {
                    Log.d("TAG", "onCancelled: ${error.message}")
                }
            }
            database.child(uid).addValueEventListener(valueEventListener)
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search