使用 firebase 未显示回收站视图 - kotlin

Posted

技术标签:

【中文标题】使用 firebase 未显示回收站视图 - kotlin【英文标题】:Recycler View is not Showing by using firebase - kotlin 【发布时间】:2021-06-18 08:36:00 【问题描述】:

我正在尝试从 firebase 获取数据并在 recyclerview 中使用

我没有将 xml 代码与此代码一起放置,因为我的 xml 100% 没问题。我认为我的 KT 有一些错误,所以我只上传 KT。

FireBase 中的数据:

    
  "movielist" : 
    "01" : 
      "director" : "Srinu Vaitla",
      "poster" : "gs://mr-mahesh-10f6d.appspot.com/Dookudu_HQ_posters_mahesh_babu_birt.jpg",
      "rating" : "gs://mr-mahesh-10f6d.appspot.com/rating.png",
      "title" : "Dookudu",
      "year" : 2020
    ,
    "02" : 
      "director" : "Srinu Vaitla",
      "poster" : "gs://mr-mahesh-10f6d.appspot.com/Dookudu_HQ_posters_mahesh_babu_birt.jpg",
      "rating" : "gs://mr-mahesh-10f6d.appspot.com/rating.png",
      "title" : "Dookudu",
      "year" : 2020
    
  

主屏幕活动:

package com.example.maheshbabu

import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.firebase.ui.database.FirebaseRecyclerOptions
import com.google.firebase.database.FirebaseDatabase
import kotlinx.android.synthetic.main.layout_main_screen.*

class MainScreenActivity : AppCompatActivity(), RecyclerViewAdapter.OnItemListener 

    private var adapter: RecyclerViewAdapter? = null

    override fun onCreate(savedInstanceState: Bundle?) 
        super.onCreate(savedInstanceState)
        setContentView(R.layout.layout_main_screen)
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.setHasFixedSize(true)
        val options: FirebaseRecyclerOptions<Holding?> = FirebaseRecyclerOptions.Builder<Holding>()
            .setQuery(
                FirebaseDatabase.getInstance().reference.child("movielist"),
                Holding::class.java
            )
            .build()

        adapter = RecyclerViewAdapter(options,this,this)
        recyclerView.adapter = adapter
    

    override fun onStart() 
        super.onStart()
        adapter!!.startListening()
    

    override fun onStop() 
        super.onStop()
        adapter!!.stopListening()
    

    override fun onItemClick(movieName: Holding, position: Int) 
        Toast.makeText(this, movieName.title, Toast.LENGTH_SHORT).show()

        intent = Intent(this, InfoScreenActivity::class.java)
//        intent.putExtra("Website",itemView.name.toString())
//        intent.putExtra("Web", position.toString())
        startActivity(intent)
    


回收站视图适配器:

package com.example.maheshbabu

import android.content.Context
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.bumptech.glide.Glide
import com.firebase.ui.database.FirebaseRecyclerAdapter
import com.firebase.ui.database.FirebaseRecyclerOptions
import com.google.android.material.imageview.ShapeableImageView
import kotlinx.android.synthetic.main.layout_main_screen_recylerview_list.view.*


internal class RecyclerViewAdapter(
    options: FirebaseRecyclerOptions<Holding?>,
    var clickListener: OnItemListener,
    private val context: Context
) :
    FirebaseRecyclerAdapter<Holding?, RecyclerViewAdapter.MyViewHolder?>(options) 

    override fun onBindViewHolder(holder: MyViewHolder, position: Int, holding: Holding) 
        holder.movieName?.text = holding.title
        holder.moviePoster?.context?.let 
            Glide.with(it).load(holding.poster).into(holder.moviePoster!!)
        
        holder.movieDirector?.text = holding.director
        holder.movieYear?.text = holding.year
        holder.movieRating?.context?.let 
            Glide.with(it).load(holding.rating).into(holder.movieRating!!)
        
    

    internal inner class MyViewHolder(itemView: View) :
        RecyclerView.ViewHolder(itemView) 
        var movieName: TextView? = itemView.title
        var moviePoster: ShapeableImageView? = itemView.poster
        var movieDirector: TextView? = itemView.director
        var movieYear: TextView? = itemView.year
        var movieRating: ImageView? = itemView.rating
        fun initialize(holderList: Holding, action: OnItemListener) 
            itemView.setOnClickListener 
                action.onItemClick(holderList, adapterPosition)
            
        

    

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder 
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.layout_main_screen_recylerview_list, parent, false)
        return MyViewHolder(view)
    


    interface OnItemListener 
        fun onItemClick(movieName: Holding, position: Int)
    

持有人:

package com.example.maheshbabu

class Holding(
    var title: String?,
    var poster: String?,
    var director: String?,
    var year: String?,
    var rating: String?
) 

LogCat:

    2021-03-22 07:47:07.453 28773-28773/? I/mple.maheshbab: Late-enabling -Xcheck:jni
2021-03-22 07:47:07.672 28773-28773/com.example.maheshbabu I/Perf: Connecting to perf service.
2021-03-22 07:47:07.689 28773-28773/com.example.maheshbabu W/ComponentDiscovery: Class com.google.firebase.dynamicloading.DynamicLoadingRegistrar is not an found.
2021-03-22 07:47:07.699 28773-28773/com.example.maheshbabu I/FirebaseApp: Device unlocked: initializing all Firebase APIs for app [DEFAULT]
2021-03-22 07:47:07.700 28773-28773/com.example.maheshbabu I/FirebaseInitProvider: FirebaseApp initialization successful
2021-03-22 07:47:07.768 28773-28773/com.example.maheshbabu W/mple.maheshbab: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
2021-03-22 07:47:07.770 28773-28773/com.example.maheshbabu W/mple.maheshbab: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
2021-03-22 07:47:07.782 28773-28773/com.example.maheshbabu W/mple.maheshbab: Accessing hidden method Landroid/graphics/FontFamily;-><init>()V (light greylist, reflection)
2021-03-22 07:47:07.783 28773-28773/com.example.maheshbabu W/mple.maheshbab: Accessing hidden method Landroid/graphics/FontFamily;->addFontFromAssetManager(Landroid/content/res/AssetManager;Ljava/lang/String;IZIII[Landroid/graphics/fonts/FontVariationAxis;)Z (light greylist, reflection)
2021-03-22 07:47:07.783 28773-28773/com.example.maheshbabu W/mple.maheshbab: Accessing hidden method Landroid/graphics/FontFamily;->addFontFromBuffer(Ljava/nio/ByteBuffer;I[Landroid/graphics/fonts/FontVariationAxis;II)Z (light greylist, reflection)
2021-03-22 07:47:07.783 28773-28773/com.example.maheshbabu W/mple.maheshbab: Accessing hidden method Landroid/graphics/FontFamily;->freeze()Z (light greylist, reflection)
2021-03-22 07:47:07.783 28773-28773/com.example.maheshbabu W/mple.maheshbab: Accessing hidden method Landroid/graphics/FontFamily;->abortCreation()V (light greylist, reflection)
2021-03-22 07:47:07.783 28773-28773/com.example.maheshbabu W/mple.maheshbab: Accessing hidden method Landroid/graphics/Typeface;->createFromFamiliesWithDefault([Landroid/graphics/FontFamily;Ljava/lang/String;II)Landroid/graphics/Typeface; (light greylist, reflection)
2021-03-22 07:47:07.813 28773-28803/com.example.maheshbabu D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2021-03-22 07:47:07.869 28773-28805/com.example.maheshbabu I/Adreno: QUALCOMM build                   : 6774a42, I60e4284429
    Build Date                       : 09/17/20
    OpenGL ES Shader Compiler Version: EV031.25.03.01
    Local Branch                     : 
    Remote Branch                    : refs/tags/AU_LINUX_ANDROID_LA.UM.7.2.R1.09.00.00.442.049
    Remote Branch                    : NONE
    Reconstruct Branch               : NOTHING
2021-03-22 07:47:07.869 28773-28805/com.example.maheshbabu I/Adreno: Build Config                     : S L 6.0.7 AArch64
2021-03-22 07:47:07.875 28773-28805/com.example.maheshbabu I/Adreno: PFP: 0x005ff113, ME: 0x005ff066
2021-03-22 07:47:07.880 28773-28805/com.example.maheshbabu I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
2021-03-22 07:47:07.880 28773-28805/com.example.maheshbabu I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
2021-03-22 07:47:07.881 28773-28805/com.example.maheshbabu I/OpenGLRenderer: Initialized EGL, version 1.4
2021-03-22 07:47:07.882 28773-28805/com.example.maheshbabu D/OpenGLRenderer: Swap behavior 2
2021-03-22 07:47:07.885 28773-28773/com.example.maheshbabu W/mple.maheshbab: Accessing hidden field Landroid/os/Trace;->TRACE_TAG_APP:J (light greylist, reflection)
2021-03-22 07:47:07.886 28773-28773/com.example.maheshbabu W/mple.maheshbab: Accessing hidden method Landroid/os/Trace;->isTagEnabled(J)Z (light greylist, reflection)
2021-03-22 07:47:07.886 28773-28773/com.example.maheshbabu W/mple.maheshbab: Accessing hidden method Landroid/os/Trace;->asyncTraceBegin(JLjava/lang/String;I)V (light greylist, reflection)
2021-03-22 07:47:07.886 28773-28773/com.example.maheshbabu W/mple.maheshbab: Accessing hidden method Landroid/os/Trace;->asyncTraceEnd(JLjava/lang/String;I)V (light greylist, reflection)
2021-03-22 07:47:07.886 28773-28773/com.example.maheshbabu W/mple.maheshbab: Accessing hidden method Landroid/os/Trace;->traceCounter(JLjava/lang/String;I)V (light greylist, reflection)

App is Running, No Error's also

【问题讨论】:

如果您尝试在适配器类中记录movieName 的值,logcat 中是否打印了一些内容? 我没听懂你……你什么意思?不......我没有在日志中打印任何内容 没有打印出来 onBindViewHolder里面如果你使用Log.d(TAG, holding.title),logcat里面有没有打印出来的东西?您是否尝试将空值分配给 Holder 类中的所有属性? 是的,只有我试过..它没有打印 【参考方案1】:

现已修复

“年”:2020 年到“年”:“2020”

"poster" : "gs://mr-mahesh..." 到图片网址

recyclerView.setHasFixedSize(true) 被移除

【讨论】:

以上是关于使用 firebase 未显示回收站视图 - kotlin的主要内容,如果未能解决你的问题,请参考以下文章

在 Firebase 实时数据库中,我如何遍历每个产品并在回收站视图中显示它们的详细信息

使用 ko.js mvc 未显示为 html 的表数据

回收站视图未显示

回收站视图项目中未显示分隔线

回收站视图未在 Fragment (Android) 上显示项目

如何根据位置从“回收”视图中删除Firebase数据库密钥