【发布时间】:2021-09-03 18:08:10
【问题描述】:
我想在 recyclerview 中显示项目节点详细信息,但出现了一些错误。我尝试了很多方法,但是我没有在数据库引用中的项目节点中获得项目的 uid。我正在尝试在 firebase 数据库参考中获取项目 uid,但无法做到。如何获取项目 uid 数据库参考对象
mainactivity code
class MainActivity : AppCompatActivity() {
lateinit var recyclerView: RecyclerView
lateinit var layoutManager: RecyclerView.LayoutManager
lateinit var RecyclerAdapter: RecyclerAdapter
lateinit var databaseReference: DatabaseReference
var database: FirebaseDatabase? = null
lateinit var auth:FirebaseAuth
var personlist = arrayListOf<Persons>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
recyclerView = findViewById(R.id.recyclerpersonview)
database = FirebaseDatabase.getInstance()
auth = FirebaseAuth.getInstance()
databaseReference=database?.reference!!.child("Users")
.child(auth.currentUser!!.uid).child("items")
databaseReference.addValueEventListener(object
:ValueEventListener{
override fun onDataChange(snapshot: DataSnapshot) {
try {
for (items in snapshot.children) {
val item1 = items.getValue(Persons::class.java)
personlist.add(item1!!)
}
}catch (e:Exception){
Toast.makeText(this@MainActivity,e.message,Toast.LENGTH_SHORT).show()
}
}
override fun onCancelled(error: DatabaseError) {
Toast.makeText(this@MainActivity,"unable to load
data",Toast.LENGTH_SHORT).show()
}
})
layoutManager = LinearLayoutManager(this)
RecyclerAdapter = RecyclerAdapter(this, personlist)
RecyclerAdapter.notifyDataSetChanged()
recyclerView.adapter = RecyclerAdapter
recyclerView.layoutManager = layoutManager
}
recycleradapter code
class RecyclerAdapter(var context: MainActivity, var personlist:
ArrayList<Persons>) :
RecyclerView.Adapter<RecyclerAdapter.Personviewholder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int):
Personviewholder {
val view =
LayoutInflater.from(parent.context).inflate(R.layout.singlerow,
parent,
false)
return Personviewholder(view)
}
override fun getItemCount(): Int {
return personlist.size
}
override fun onBindViewHolder(holder: Personviewholder, position:
Int) {
val view = personlist[position]
holder.personname.text = view.personname
holder.persondesc.text = view.persondesc
Picasso.with(context).load(view.personimage).into(holder.personimg)
holder.layoutlist.setOnClickListener {
val intent = Intent(context, Additem::class.java)
context.startActivity(intent)
}
}
inner class Personviewholder(view: View) :
RecyclerView.ViewHolder(view)
{
var personname: TextView = view.findViewById(R.id.tvname)
var persondesc: TextView = view.findViewById(R.id.tvpersondesc)
var personimg: ImageView = view.findViewById(R.id.imgperson)
var layoutlist: LinearLayout =
view.findViewById(R.id.linearlayoutlist)
}
}
data class
data class Persons( var personname:String,
var persondesc:String,
var personimage:String)
debug information
06/21 22:05:29: Launching 'app' on Pixel 3 API 30.
$ adb shell am start -n
"com.a.recyclerview/com.a.recyclerview.LoginActivity" -a
android.intent.action.MAIN -c android.intent.category.LAUNCHER -D
Waiting for application to come online: com.a.recyclerview |
com.a.recyclerview.test
Waiting for application to come online: com.a.recyclerview |
com.a.recyclerview.test
Connecting to com.a.recyclerview
Capturing and displaying logcat messages from application. This
behavior can be disabled in the "Logcat output" section of the
"Debugger" settings page.
I/System.out: Debugger has connected
waiting for debugger to settle...
Connected to the target VM, address: 'localhost:8602', transport:
'socket'
I/chatty: uid=10155(com.a.recyclerview) identical 6 lines
I/System.out: waiting for debugger to settle...
I/System.out: debugger has settled (1337)
D/NetworkSecurityConfig: No Network Security Config specified, using
platform default
D/NetworkSecurityConfig: No Network Security Config specified, using
platform default
I/FirebaseApp:设备解锁:为应用初始化所有 Firebase API [默认] I/FirebaseAuth: [FirebaseAuth:] 准备创建服务 与回退实现的连接 D/FirebaseAuth:通知 id 令牌侦听器有关用户( rwy6MpJLxYWyIAQbj2xFR0sRaRw2)。 I/FirebaseInitProvider:FirebaseApp 初始化成功 D/libEGL:加载/vendor/lib/egl/libEGL_emulation.so D/libEGL:加载 /vendor/lib/egl/libGLESv1_CM_emulation.so I/DynamiteModule:考虑本地模块 com.google.android.gms.measurement.dynamite:51 和远程模块 com.google.android.gms.measurement.dynamite:51 选择的远程版本 com.google.android.gms.measurement.dynamite,版本 >= 51 V/DynamiteModule: Dynamite loader version >= 2, using loadModule2NoCrashUtils D/libEGL:加载 /vendor/lib/egl/libGLESv2_emulation.so W/.a.recyclervie: 访问隐藏方法 Landroid/view/View;- > computeFitSystemWindows(Landroid/graphics/Rt;Landroid/graphics /矩形;)Z
(greylist, reflection, allowed)
Accessing hidden method Landroid/view/ViewGroup;-
makeOptionalFitsSystemWindows()V(灰名单,反射,允许) V/FA: onActivityCreated W/.a.recyclervie: 验证 android.content.pm.ApplicationInfo cn.a(java.lang.String, int) 耗时 170.667ms (64.45 bytecodes/s) (984B 近似峰值分配) V/FA:启用应用测量收集 V/FA:为应用程序包启用应用程序测量,谷歌应用程序 ID: com.a.recyclerview, 1:14819963865:android:0fbd8c80422b89f216dfc8 I/FA:应用测量已初始化,版本:42020 启用调试日志运行: adb shell setprop log.tag.FA VERBOSE I/FA:要启用更快的调试模式事件日志记录,请运行: adb shell setprop debug.firebase.analytics.app com.a.recyclerview D/FA:启用调试级消息记录 V/FA: onActivityCreated V/FA:连接到远程服务 D/HostConnection: HostConnection::get() 新主机连接 建立 0xf580da60, tid 7482 D/HostConnection:HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer ANDROID_EMU_vulkan_ignored_handles ANDROID_EMU_vulkan_free_memory_sync ANDROID_EMU_vulkan_shader_float16_int8 ANDROID_EMU_vulkan_async_queue_submit ANDROID_EMU_sync_buffer_data GL_OES_EGL_image_external_essl3 GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_async_frame_commands ANDROID_EMU_gles_max_version_3_0 W/OpenGLRenderer:无法选择配置 EGL_SWAP_BEHAVIOR_PRESERVED,重试不... D/EGL_emulation:eglCreateContext:0xf580cb80:maj 3 min 0 rcv 3 D/EGL_emulation: eglMakeCurrent: 0xf580cb80: ver 3 0 (tinfo 0xc53c7b90) (第一次) V/FA:连接尝试已在进行中 I/Gralloc4:不支持映射器 4.x D/HostConnection: createUnique: 调用 D/HostConnection: HostConnection::get() 新主机连接 建立 0xf580dc20, tid 7482 D/goldfish-address-space: allocate: 请求大小为 0x100 的块 allocate: ioctl allocate 返回的偏移量 0x3fbc4d000 大小 0x2000 V/FA:连接尝试已在进行中 V/FA:活动恢复,时间:3149571 D/HostConnection:HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer ANDROID_EMU_vulkan_ignored_handles ANDROID_EMU_vulkan_free_memory_sync ANDROID_EMU_vulkan_shader_float16_int8 ANDROID_EMU_vulkan_async_queue_submit ANDROID_EMU_sync_buffer_data GL_OES_EGL_image_external_essl3 GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_async_frame_commands ANDROID_EMU_gles_max_version_3_0 I/FA:未找到跟踪代码管理器,因此不会使用 V/FA:连接尝试已在进行中 V/FA:连接尝试已在进行中 I/OpenGLRenderer:戴维!持续时间=757ms;标志=1, 预期Vsync=3149655912401,Vsync=3149722579065, 最旧输入事件=9223372036854775807,最新输入事件=0, HandleInputStart=3149736734700, AnimationStart=3149737212900, PerformTraversalsStart=3149737532500,DrawStart=3150142325100, SyncQueued=3150160621300,SyncStart=3150229754900, IssueDrawCommandsStart=3150230091100,SwapBuffers=3150464728600, FrameCompleted=3150482529700, DequeueBufferDuration=422700, QueueBufferDuration=1357200, GpuCompleted=72904454231491230, D/FA:连接到远程服务 V/FA:处理排队服务任务:5 I/.a.recyclervie: WaitForGcToComplete 阻塞了 RunEmptyCheckpoint ProfileSaver 为 5.416 毫秒 D/CompatibilityChangeReporter:Compat change id 报告:147798919; 用户标识符 10155;状态:启用 V/FA:不活动,与服务断开连接

【问题讨论】:
-
也发布你的数据库结构
标签: android firebase firebase-realtime-database android-recyclerview