简体   繁体   English

类型不匹配。 必需:找到的上下文:在片段中

[英]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 onViewCreatedonViewCreated被调用

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
  1. Do not pass the context to the adapter.不要将上下文传递给适配器。 Remove it, you don't need it.删除它,你不需要它。

  2. 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)

  3. 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.

相关问题 类型不匹配。 必需:片段,找到:PlaceAutocompleteFragment - Type mismatch. Required: Fragment, Found: PlaceAutocompleteFragment 类型不匹配。 所需上下文:找到 MyAdapter.ViewHolder - Type mismatch. Required Context: Found MyAdapter.ViewHolder 类型不匹配。 要求:观察员<in Int!>成立:? - Type mismatch. Required: Observer<in Int!> Found:? 类型不匹配。 必需:找不到:回调&lt;*&gt; - Type mismatch. Required: Nothing Found: Callback<*> 类型不匹配。 必需:ContentResolver! 找到:整数 - Type mismatch. Required: ContentResolver! Found: Int 类型不匹配。 必需:数组 <Uri> ! 找到:数组 <Uri?> - Type mismatch. Required: Array<Uri>! Found: Array<Uri?> 类型不匹配。 必需:结果<newsresponse> : 发现: 结果<response<newsresponse> &gt;? </response<newsresponse></newsresponse> - Type mismatch. Required: Result<NewsResponse>! Found: Result<Response<NewsResponse>>? 类型不匹配。 必需:FirebaseRecyclerAdapter<chatobject, chatvoiceviewholders> ? 成立:</chatobject,> - Type mismatch. Required: FirebaseRecyclerAdapter<ChatObject, ChatVoiceViewHolders>? Found: 类型不匹配。 必需: suspend () → Response&lt;*&gt; 找到:Response<Void> - Type mismatch. Required: suspend () → Response<*> Found: Response<Void> 类型不匹配。 必需:字符串:找到:Int 错误 - Type mismatch. Required: String! Found: Int error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM