[英]How can I put images on my RecyclerView from my Firebase database using Picasso?
I want to populate my RecyclerView with images that I have on my firebase database.我想用我的 firebase 数据库中的图像填充我的 RecyclerView。 I am using Groupie for my RecyclerView adapter.
我正在为我的 RecyclerView 适配器使用 Groupie。 My adapter class looks like this
我的适配器 class 看起来像这样
class HobbiesAdapter(val hobbyItem: HobbiesClass): Item<GroupieViewHolder>(){
override fun bind(viewHolder: GroupieViewHolder, position: Int) {
viewHolder.itemView.hobbynameTV.text = hobbyItem.hobbyName
//viewholder.itemView.hobbyImageView ...
}
override fun getLayout(): Int {
return R.layout.row
}
}
@Parcelize
class HobbiesClass (val hobbyName:String):Parcelable{
constructor():this("")
}
Here is my RecyclerView item row xml file:这是我的 RecyclerView 项目行 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="164dp"
android:layout_height="70dp"
android:layout_marginStart="5dp"
android:background="@android:color/transparent"
xmlns:android="http://schemas.android.com/apk/res/android">
<FrameLayout
android:id="@+id/frameHobby"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/hobbiesbackground">
<TextView
android:id="@+id/hobbynameTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Camping"
android:textColor="#000000"
android:fontFamily="@font/extralight"
android:layout_gravity="center|right"
android:layout_marginEnd="15dp"/>
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/hobbyImageView"
android:layout_width="54dp"
android:layout_height="47dp"
android:layout_gravity="center|left"
android:layout_marginStart="12dp"/>
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
I can retrieve hobbies names by this piece of code here我可以通过这段代码在这里检索爱好名称
val reference = database.getReference("Hobbies")
reference.addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
val adapter = GroupAdapter<GroupieViewHolder>()
for (snap in snapshot.children) {
val hobbiesItem = snap.getValue(HobbiesClass::class.java)
if (hobbiesItem != null) {
adapter.add(HobbiesAdapter(hobbiesItem))
}
}
tophobbies.adapter = adapter
}
override fun onCancelled(error: DatabaseError) {
}
})
Here is my Realtime Database这是我的实时数据库
I tried putting this code block under adapter.add(HobbiesAdapter(hobbiesItem))我尝试将此代码块放在 adapter.add(HobbiesAdapter(hobbiesItem)) 下
for (dataSnapshot in snapshot.children){
var map = dataSnapshot.getValue() as Map<String,Object>
val imageLink = map.get("imageUrl") as String
Picasso.get().load(imageLink).into(hobbyImageView)
}
But it only put image to my first item in my RecyclerView also its not the correct image.但它只将图像放在我的 RecyclerView 中的第一个项目中,它也不是正确的图像。 Here is how my app looks like after I launch it
这是我的应用启动后的样子
Your problem is here:你的问题在这里:
if (hobbiesItem != null) {
adapter.add(HobbiesAdapter(hobbiesItem))
}
Here you are passing one hobbiesItem, and not a list of hobbiesItem to the Adapter.这里您传递的是一个 hobbiesItem,而不是一列 hobbiesItem 到适配器。
The adapter should accept a list of hobbiesItem.适配器应该接受一个 hobbiesItem 列表。 And onDataChange should pass that list to the adapter.
onDataChange 应该将该列表传递给适配器。
I have found a solution.我找到了解决办法。 It was the first thing I wanted to do.
这是我想做的第一件事。 But I was just missing something.
但我只是错过了一些东西。 What I did I added new string parameter to HobbiesClass class called imageUrl(this has to be the same name with the name of your key in your Firebase database)
我做了什么 我向 HobbiesClass class 添加了新的字符串参数,称为 imageUrl(这必须与 Firebase 数据库中的密钥名称相同)
Hobbies class兴趣爱好 class
@Parcelize
class HobbiesClass (val hobbyName:String,val imageUrl:String):Parcelable{
constructor():this("","")
}
Then I added然后我补充说
Picasso.get().load(hobbyItem.imageUrl).into(viewHolder.itemView.hobbyImageView)
In my bind method of my HobbiesAdapter class. I did not change anything in my Hobbies class. Now it works perfectly fine.在我的 HobbiesAdapter class 的绑定方法中。我没有更改我的 Hobbies class 中的任何内容。现在它工作得很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.