gpt4 book ai didi

android - RecyclerView上 Unresolved reference

转载 作者:行者123 更新时间:2023-12-03 04:11:51 34 4
gpt4 key购买 nike

我能够从Firebase检索数据,但是当我尝试将其加载到groupie适配器中时,recyclerview Activity 未加载,并显示以下错误“ Unresolved reference :由于接收器类型不匹配,以下候选者均不适用....“,我是否缺少依赖关系??

这是我的 Activity 代码,在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
}

}



这是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'

这是我的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>

解决方案:
最终这是一个非常愚蠢的错误,我意识到我已经将fetchUsers()方法留在NewMessageActivity类之外,所以我将该方法移回了类中,并且能够引用RecyclerView组件。

最佳答案

在设置适配器之前,使用以下代码定义对RecyclerView的引用。即

  • 在类中位于它上方的onCreate函数外部。如下定义您的RecyclerView
    lateinit var recycler_view_new_message: RecyclerView
  • recycler_view_new_message.adapter =适配器之前的onCreate函数内部,添加以下代码。
    recycler_view_new_message = findViewById<RecyclerView>(R.id. recycler_view_new_message)
  • 关于android - RecyclerView上 Unresolved reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59523566/

    34 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com