[英]Type mismatch. Required: Context Found: In Fragment
In my SearchFragment.kt
I have:在我的
SearchFragment.kt
我有:
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
在
onViewCreated
被调用
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
getUsers()
}
My UserAdapter.kt
look like:我的
UserAdapter.kt
看起来像:
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:在
SearchFragment.kt
中的以下行:
adapter = UsersAdapters(this, users)
Im getting the following error:我收到以下错误:
Type mismatch.
Required:
Context
Found:
SearchFragment
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.扩充视图时,请使用
parent.context
而不是外部上下文。 you can also inline that return statement, like.您还可以内联该 return 语句,例如。
return LayoutInflater.from(parent.context).inflate(R.layout.item_user, parent, false)返回 LayoutInflater.from(parent.context).inflate(R.layout.item_user, parent, false)
You are using Glide, within your ViewHolder class,您在 ViewHolder 类中使用 Glide,
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. Glide 会将视图作为参数。 That works and it is the right way to write your code.
这是有效的,它是编写代码的正确方法。
尝试这个:
adapter = UsersAdapters(requireActivity(), users)
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.