Reputation: 2017
In my SearchFragment.kt
I have:
private lateinit var userDb: FirebaseFirestore
private lateinit var users: MutableList<User>
private lateinit var adapter: UsersAdapters
fun getUsers() {
users = mutableListOf()
adapter = UsersAdapters(this, users)
userDb = FirebaseFirestore.getInstance()
val userReference = userDb.collection("users")
userReference.addSnapshotListener { snapshot, exception ->
if (exception != null || snapshot == null) {
Log.i("TAG", "Somethings Wrong", exception)
return@addSnapshotListener
}
val userList = snapshot.toObjects(User::class.java)
for (user in userList) {
Log.i("TAG", "Document ${user.username}")
}
}
}
Which gets called in in onViewCreated
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
getUsers()
}
My UserAdapter.kt
look like:
package com.example.myapp
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import kotlinx.android.synthetic.main.item_user.view.*
import com.example.myapp.models.User
class UsersAdapters (val context: Context, val users: List<User>) :
RecyclerView.Adapter<UsersAdapters.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(context).inflate(R.layout.item_user, parent, false)
return ViewHolder(view)
}
override fun getItemCount() = users.size
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(users[position])
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bind(user: User) {
itemView.etUsername.text = user?.username
Glide.with(context).load(user.profileURL).into(itemView.profileImage)
}
}
}
In SearchFragment.kt
on the following line:
adapter = UsersAdapters(this, users)
Im getting the following error:
Type mismatch.
Required:
Context
Found:
SearchFragment
Upvotes: 1
Views: 4556
Reputation: 1
In my case
class ModeradorFragment: Fragment() {
override fun onAttach(context: Context) {
Log.d(Constraints.TAG, "onAttach")
super.onAttach(context)
}
I put the on attach at the beginning of the class then use it in my linear layout
val layoutManager1 = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
val adapterpreg = PreguntasAdapter(this@ModeradorFragment, data)
Upvotes: 0
Reputation: 2425
Do not pass the context to the adapter. Remove it, you don't need it.
When inflating your view, use parent.context
instead of the outer context. you can also inline that return statement, like.
return LayoutInflater.from(parent.context).inflate(R.layout.item_user, parent, false)
You are using Glide, within your ViewHolder class,
replace
Glide.with(context).load(user.profileURL).into(itemView.profileImage)
with
Glide.with(itemView).load(user.profileURL).into(itemView.profileImage)
Glide will take the view as a parameter. That works and it is the right way to write your code.
Upvotes: 1