简体   繁体   English

如何使用毕加索将我的 Firebase 数据库中的图像放到我的 RecyclerView 上?

[英]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 到适配器。

  1. Create a global list of type hobbiesItem.创建类型为 hobbiesItem 的全局列表。
  2. Replace the adapter.add(HobbiesAdapter(hobbiesItem)) to exampleList.add(HobbiesAdapter(hobbiesItem))将 adapter.add(HobbiesAdapter(hobbiesItem)) 替换为 exampleList.add(HobbiesAdapter(hobbiesItem))
  3. And then pass this exampleList to the adapter like this: adapter.add(exampleList).然后像这样将这个 exampleList 传递给适配器:adapter.add(exampleList)。

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.

相关问题 如何在我的 RecyclerView 中显示来自 firebase 的子数据? - How do I display child of child's data from firebase in my RecyclerView? 如何使用云函数从我的 Firebase 实时数据库中 database.ref 正在侦听的节点内部获取数据? - How can I fetch data from inside a node which the database.ref is listening to in my Firebase real-time database using cloud functions? 使用 Firebase 使用我的本地图像创建数据库 - Create a database with my local images with Firebase 无法使用 Picasso 的 URL 从 Firebase 数据库加载图像 - Unable to load image from Firebase Database using an URL with Picasso 我可以将 firebase 数据库规则更新到我的机器中吗? - Can I update the firebase database rules into my machine? 为什么我的图像没有从 firebase 火库中显示? - Why my images is not showing from firebase firestore? 如何从 javascript 中的 firebase 实时数据库中获取子节点的总数? - How do I fetch the total number of child nodes from my firebase realtime database in javascript? 如何从 function 返回我的 Firebase 实时数据库的快照值? - How do I return the snapshot value of my Firebase Realtime Database from a function? 如何从 TypeScript 中的可调用 Firebase Cloud Function 向我的 Unity 应用程序发送自定义 object? - How can I send custom object from my callable Firebase Cloud Function in TypeScript to my Unity app? 我如何将我的 firebase 实时数据库 function 转换为 firestore function - How can i convert my firebase realtime database function to firestore function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM