RecycleView 不从 Google Firestore 检索数据

Posted

技术标签:

【中文标题】RecycleView 不从 Google Firestore 检索数据【英文标题】:RecycleView does not retrieve data from Google Firestore 【发布时间】:2021-05-21 20:50:49 【问题描述】:

我需要在 Kotlin 中使用 RecycleView 的帮助。我连接了我的 Google Firestore,并且我所有的 CRUD 操作都在工作。现在我想查看我添加到 Firebase 的那些创建的电影 Titles,但 RecycleView 没有显示任何我的 Firestore 数据。

NewestTitlesFragment:

package com.example.watchlist.sampledata

class NewestTitlesFragment : Fragment() 

    private val auth = FirebaseAuth.getInstance()
    private val currentUser = auth.currentUser
    private val db:FirebaseFirestore = FirebaseFirestore.getInstance()
    private val collectionReference:CollectionReference = db.collection("users").document(currentUser!!.uid).collection("titles")

    var titleAdapter: TitleAdapter? = null

    lateinit var binding: FragmentNewestTitlesBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ) =FragmentNewestTitlesBinding.inflate(inflater, container, false).run 
        binding = this
        val query : Query = collectionReference
        val firestoreRecyclerOptions: FirestoreRecyclerOptions<Watch> = FirestoreRecyclerOptions.Builder<Watch>()
            .setQuery(query, Watch::class.java)
            .build()

        titleAdapter = TitleAdapter(firestoreRecyclerOptions)
        newestTitlesRecycleView.layoutManager = LinearLayoutManager(context)
        newestTitlesRecycleView.adapter = titleAdapter

        root
    



TitleAdapter:

    package com.example.watchlist
    
    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 com.firebase.ui.firestore.FirestoreRecyclerAdapter
    import com.firebase.ui.firestore.FirestoreRecyclerOptions
    
    
    class TitleAdapter(options: FirestoreRecyclerOptions<Watch>) :
        FirestoreRecyclerAdapter<Watch, TitleAdapter.TitleAdapterVH>(options) 
        
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TitleAdapterVH 
            return TitleAdapterVH(LayoutInflater.from(parent.context).inflate(R.layout.titles_layout, parent, false))
        
    
        override fun onBindViewHolder(holder: TitleAdapterVH, position: Int, model: Watch) 
    
            holder.movieTitle.text = model.title
    
        
    
        class TitleAdapterVH(itemView: View) : RecyclerView.ViewHolder(itemView) 
    
            var movieTitle = itemView.findViewById<TextView>(R.id.movieTitle)
            var moviePoster = itemView.findViewById<ImageView>(R.id.titlePoster)
        
    

观看:

    package com.example.watchlist
    
    import android.net.Uri
    
    data class Watch(
        val id : Int,
        var title :String ,
        var content:String,
        var date:String,
        var img:String ,
        )

titles_layout.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_
        android:layout_>
    
        <LinearLayout
            android:layout_
            android:layout_
            android:orientation="vertical">
    
            <ImageView
                android:id="@+id/titlePoster"
                android:layout_
                android:layout_
                tools:srcCompat="@tools:sample/avatars" />
    
            <TextView
                android:id="@+id/movieTitle"
                android:layout_
                android:layout_
                android:paddingLeft="20dp"
                android:text="TextView"
                android:textStyle="bold" />
        </LinearLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>

fragment_newest_titles.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_
        android:layout_
        android:background="#FAF7F7"
        tools:context=".sampledata.NewestTitlesFragment">
    
        <LinearLayout
            android:layout_
            android:layout_
            android:orientation="vertical">
    
            <ImageView
                android:id="@+id/imageView5"
                android:layout_
                android:layout_
                android:layout_alignBottom="@id/newestTitlesRecycleView"
                android:background="#000000"
                app:srcCompat="@drawable/watchlist_logo" />
    
            <TextView
                android:id="@+id/textView3"
                android:layout_
                android:layout_
                android:background="#FF0000"
                android:gravity="center"
                android:text="Newest Titles"
                android:textColor="#FFFFFF"
                android:textIsSelectable="false"
                android:textSize="24sp"
                android:textStyle="bold" />
    
            <LinearLayout
                android:layout_
                android:layout_
                android:gravity="bottom"
                android:orientation="horizontal">
    
                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/newestTitlesRecycleView"
                    android:layout_
                    android:layout_
                    android:background="#8A8A8A"
                    android:numColumns="2"
                    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
            </LinearLayout>
    
        </LinearLayout>
    
    </androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

请编辑您的问题并将您的数据库结构添加为屏幕截图。 把这行 holder.movi​​eTitle.text = model.title 改为 holder.movi​​eTitle.text = model.get(position).title 它没有帮助,我将 holder.movi​​eTitle.text = model.title 更改为 holder.movi​​eTitle.text = model.get(position).title 并没有帮助。 【参考方案1】:

如果不了解您使用的环境,这些类型的问题可能很难诊断。最好尽可能在本地进行测试,并查看部署过程中产生的任何错误日志。

有一些出色的工具可以在本地测试您的 Firebase 代码。如果您以增量方式unit test 代码和interactively from a local shell,您将很快发现任何问题。

您还可以使用Firebase Emulator 对您的代码进行原型设计、开发和测试,然后再将其投入生产。

【讨论】:

以上是关于RecycleView 不从 Google Firestore 检索数据的主要内容,如果未能解决你的问题,请参考以下文章

RecycleView实现侧滑删除item

安卓开发笔记 RecycleView

RecycleView 点击事件

RecycleView 点击事件

Android RecycleView自定义布局的使用

关于 RecycleView [关闭]