如何解决使用 firebase 未显示 Recycler View - kotlin
我正在尝试从 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","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?
)
日志猫:
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
解决方法
现已修复
“年”:2020 到“年”:“2020”
"poster" : "gs://mr-mahesh..." 到图片网址
recyclerView.setHasFixedSize(true) 被移除
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。