I'm showing a List of users in my Firebase Database using the popular Groupie Library to generate the Recycler View. Inside the ViewHolder class's bind() [onBindViewHolder() without using Groupie], I'm trying to get the Dominant Color from the Palette created from the user's Profile Image & set that color as the Imageview's Shadow Color.
However, to use the palette, I need to provide context in the
palette.getDominantColor(ContextCompat.getColor(context, R.color.defaultColor))
Problem is, how do I get this context inside the bind() function. If I try to pass the context in the function, it loses the Override & I get the error 'bind' overrides nothing.
Here is my code to get better understanding of the problem.
class FirebaseSearchActivity: AppCompatActivity() {
//Get Firebase Database Reference
private val dbRef = FirebaseDatabase.getInstance().getReference("/users")
override fun onCreate(savedInstanceState: Bundle ? ) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_firebase_search)
val searchToolbar: Toolbar = findViewById(R.id.search_toolbar)
setSupportActionBar(searchToolbar)
supportActionBar ? .setDisplayShowTitleEnabled(false)
getUserResults()
}
private fun getUserResults() {
dbRef.addListenerForSingleValueEvent(object: ValueEventListener {
override fun onDataChange(p0: DataSnapshot) {
//Setup Groupie Adapter
val adapter = GroupAdapter < ViewHolder > ()
//Fetch Snapshot of Database
p0.children.forEach {
Log.d("FetchedUsers", "$it")
//Assign Fetched Data to Model Class
val user = it.getValue(User::class.java)
//Null Check
if (user != null) {
adapter.add(ResultItem(user, baseContext))
}
}
//Attach Groupie Adapter to the Recycler View
firebase_result_list.adapter = adapter
}
override fun onCancelled(p0: DatabaseError) {
//Show Error
Log.d("FetchedUsers", "$p0")
Toast.makeText(baseContext, "$p0", Toast.LENGTH_LONG).show()
}
})
}
}
//ViewHolder
class ResultItem(val user: User, ctx: Context): Item < ViewHolder > () {
//Setup List Row Layout to View Holder
override fun getLayout(): Int {
return R.layout.search_list_layout
}
//Change values of text fields or image views inside the list's layout
override fun bind(viewHolder: ViewHolder, position: Int) {
//Load Username
Log.d("FetchedUserName", "Username: ${user.username}")
viewHolder.itemView.search_list_username.text = user.username
//Load Profile Pic
Glide.with(viewHolder.itemView)
.asBitmap()
.load(user.profileImage)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.listener(object: RequestListener < Bitmap > {
override fun onLoadFailed(
e: GlideException ? ,
model : Any ? ,
target : com.bumptech.glide.request.target.Target < Bitmap > ? ,
isFirstResource : Boolean
): Boolean {
return false
}
override fun onResourceReady(
resource: Bitmap ? ,
model : Any ? ,
target : com.bumptech.glide.request.target.Target < Bitmap > ? ,
dataSource : DataSource ? ,
isFirstResource : Boolean
): Boolean {
if (resource != null) {
val p = Palette.from(resource).generate()
// Use generated instance
viewHolder.itemView.search_list_profile.shadowColor = p.getDominantColor(ContextCompat.getColor(ctx, R.color.defaultColor))
}
return false
}
})
.into(viewHolder.itemView.search_list_profile)
}
}
Declare your
ctx:Context
as this
private val ctx:Context
This way will able to access content in whole class.