Issue
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
Solution
Try this:
adapter = UsersAdapters(requireActivity(), users)
Answered By - MidasLefko
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.