简体   繁体   English

RecyclerView 上未解析的参考

[英]Unresolved reference on RecyclerView

I'm able to retrieve the data from Firebase but when I try to load it into the groupie adapter, the recyclerview activity is not loading, and shows the following error "Unresolved reference: None of the following candidates are applicable because of receiver type mismatch....", I am I missing dependency??我能够从 Firebase 检索数据,但是当我尝试将其加载到 groupie 适配器时,recyclerview 活动未加载,并显示以下错误“未解析的参考:由于接收器类型不匹配,以下候选者均不适用....”,我是不是缺少依赖?

This is my activity code, under to the fetchUser method is where I get the error:这是我的活动代码,在 fetchUser 方法下是我收到错误的地方:


import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.google.firebase.database.DataSnapshot
import com.google.firebase.database.DatabaseError
import com.google.firebase.database.FirebaseDatabase
import com.google.firebase.database.ValueEventListener
import com.xwray.groupie.GroupAdapter
import com.xwray.groupie.GroupieViewHolder
import com.xwray.groupie.Item
import kotlinx.android.synthetic.main.activity_new_message.*

class NewMessageActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_new_message)


        // Activity screen title
        supportActionBar?.title = "Select User"

        val adapter = GroupAdapter<GroupieViewHolder>()


        adapter.add(UserItem())
        adapter.add(UserItem())
        adapter.add(UserItem())

        recycler_view_new_message.adapter = adapter

        fetchUsers()

    }
}

private fun fetchUsers() {
    val ref = FirebaseDatabase.getInstance().getReference("/users")
    ref.addListenerForSingleValueEvent(object: ValueEventListener {

        override fun onDataChange(p0: DataSnapshot) {
            val adapter = GroupAdapter<GroupieViewHolder>()


            p0.children.forEach {
                Log.d("NewMessage", it.toString())
                val user = it.getValue(User::class.java)
                adapter.add(UserItem())
            }
            recycler_view_new_message.adapter = adapter /// THIS IS WHERE I GET THE UNRESOLVED REFERENCE ERROR
        }

        override fun onCancelled(p0: DatabaseError) {

        }
    })


}

class UserItem: Item<GroupieViewHolder>() {
    override fun bind(viewHolder: GroupieViewHolder, position: Int) {
    }

    override fun getLayout(): Int {
        return R.layout.user_row_new_message
    }

}


This is the gradle dependencies:这是 gradle 依赖项:

    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'

    implementation 'com.google.firebase:firebase-analytics:17.2.0'
    implementation 'com.google.firebase:firebase-auth:19.2.0'

    implementation 'com.google.firebase:firebase-storage:19.1.0'
    implementation 'com.google.firebase:firebase-database:19.1.0'
    implementation 'de.hdodenhof:circleimageview:3.0.1'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    implementation "com.xwray:groupie:2.7.0"

    implementation 'com.squareup.picasso:picasso:2.71828'


}

apply plugin: 'com.google.gms.google-services'

and this is my recyclerView XML config:这是我的 recyclerView XML 配置:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".NewMessageActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view_new_message"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

SOLUTION: It ended up being a very dumb mistake, I realized I had left fetchUsers() method outside of the NewMessageActivity class, so I just moved that method back into the class and I was able to reference the RecyclerView component.解决方案:结果是一个非常愚蠢的错误,我意识到我将 fetchUsers() 方法留在了 NewMessageActivity 类之外,所以我只是将该方法移回类中,并且我能够引用 RecyclerView 组件。

Define the reference to the RecyclerView using the code below before setting an adapter to it.在为其设置适配器之前,使用以下代码定义对RecyclerView的引用。 ie IE

  1. Outside onCreate function right above it within the class.在类中它正上方的onCreate函数之外。 Define your RecyclerView as below.如下定义您的RecyclerView

    lateinit var recycler_view_new_message: RecyclerView

  2. Inside your onCreate function before recycler_view_new_message.adapter = adapter , add the code below.recycler_view_new_message.adapter = adapter之前的onCreate函数中,添加以下代码。

    recycler_view_new_message = findViewById<RecyclerView>(R.id. recycler_view_new_message)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM