i am trying to get data from firebase and use in recyclerview
i am not placing xml code along with this code because my xml is 100% fine. i think there is some mistake in my KT so i am uploading only KT.
Data in 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
}
}
}
Main Screen Activity:
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)
}
}
Recycler View Adapter :
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)
}
}
Holder :
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)
Now Fixed
"year" : 2020 to "year" : "2020"
"poster" : "gs://mr-mahesh..." to Image URL
recyclerView.setHasFixedSize(true) is removed