我能够从Firebase检索数据,但是当我尝试将其加载到groupie适配器中时,recyclerview活动未加载,并显示以下错误“未解析的参考:由于以下原因,以下候选者均不适用:接收器类型不匹配...。”,我缺少依赖项吗?
这是我的活动代码,在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>
答案 0 :(得分:-1)
在设置适配器之前,使用以下代码定义对RecyclerView
的引用。即
外部onCreate
函数在类中位于其上方。如下定义您的RecyclerView
。
lateinit var recycler_view_new_message: RecyclerView
在您的onCreate
函数中,在 recycler_view_new_message.adapter = adapter 之前,添加以下代码。
recycler_view_new_message = findViewById<RecyclerView>(R.id. recycler_view_new_message)