简体   繁体   中英

How to load in a picture from firebase storage into recyclerview?

at the moment in my code when a user registers an account they select their profile picture. This then stores in firebase storage as a picture and i also store the image url as a string in my database. In the app they can create a post and when i display the post i wish to load in their profile picture. At the moment i have all the post details displaying I'm just wondering how to load in their profile picture in the onBindViewHolder(). Any help or suggestions would be appreciated thanks!

MyAdapter to display the post looks like this:

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import ie.wit.savvytutor.R
import ie.wit.savvytutor.fragments.post
import ie.wit.savvytutor.fragments.user
import ie.wit.savvytutor.helpers.readImageFromPath
import ie.wit.savvytutor.models.PostModel
import ie.wit.savvytutor.models.UserModel
import org.w3c.dom.Text


class DisplayPostAdapter(private val postList: ArrayList<PostModel>) : 
RecyclerView.Adapter<DisplayPostAdapter.PostViewHolder>(){

public var postId:String = ""


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PostViewHolder {
    val itemView = 
LayoutInflater.from(parent.context).inflate(R.layout.displaypost_view,parent,false)
    return PostViewHolder(itemView)
}

override fun onBindViewHolder(holder: PostViewHolder, position: Int) {
    val currentItem = postList[position]

    holder.title.text = currentItem.title
    holder.subject.text = currentItem.subject
    holder.location.text = currentItem.location
    holder.level.text = currentItem.level
    holder.description.text = currentItem.description
    holder.username.text = currentItem.email
    holder.profilepic.setImageResource(user.profilepic)



}

override fun getItemCount(): Int {
    return postList.size
}

class PostViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView){

    val title : TextView = itemView.findViewById(R.id.displayTitle)
    val subject : TextView = itemView.findViewById(R.id.displaySubject)
    val location :TextView = itemView.findViewById(R.id.displayLocation)
    val level : TextView = itemView.findViewById(R.id.displayLevel)
    val description : TextView = itemView.findViewById(R.id.displayDescription)
    var profilepic: ImageView = itemView.findViewById(R.id.displayProfilePic)
    val username: TextView = itemView.findViewById(R.id.displayParentName)

}

fun test(position: Int){

    val currentItem = postList[position]
    postId = currentItem.postId
    // println("post id from adapter = " + postId)
}



fun deleteItem(pos:Int){
    //send this postid to the handler
    test(pos)
    postList.removeAt(pos)
    notifyItemRemoved(pos)
}
}

It stores in my database like this

Image stored in firebase storage

You can also use it as extension function & you need Glide dependency to load image from url

and just replace this:-

holder.profilepic.setImageResource(user.profilepic)

with:-

holder.profilepic.setImage(user.profilepic)

Your extension function

fun ImageView.setImage(url: String) {
            Glide.with(this).load(url)
                .error(ContextCompat.getDrawable(context, R.drawable.placeholder)).into(this)
       
    }

Solved using a for loop that gets the usersid and then there profile pic. Then making sure when the user creates a post it passes in their id with the post. Then matching their id to the one in the post. When i got their id and matched it then got their profile picture and used Picasso to pass the the profile picture into the holder. From my research glide would have also worked so other solutions on this post may also work. Here is the code that i used to get mine, sorry if this was hard to follow!

    val dbRef = FirebaseDatabase.getInstance("DATABASE LINK")
            .getReference("Users")
    mAuth = FirebaseAuth.getInstance()

    dbRef.addValueEventListener(object : ValueEventListener {
        override fun onDataChange(snapshot: DataSnapshot) {
            if (snapshot.exists()) {
                for (userSnapshot in snapshot.children) {
                    val users = userSnapshot.getValue(UserModel::class.java)
                    val individualDb = currentItem.uid?.let { dbRef.child(it) }
                    //println("indavidual users database " + individualDb)
                    var user: String = ""
                    if (individualDb != null) {
                        
 individualDb.child("profilepic").get().addOnSuccessListener {
                            if (it.exists()) {
                                var link = it.value
                                //println("THIS IS LINK    " + link)
                                Picasso.get().load(link.toString())
                                    .into(holder.displayProfilePic);
                            }
                        }

                    }


                }
            }
        }

        override fun onCancelled(error: DatabaseError) {
        }
    })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM