简体   繁体   English

如何使用 Kotlin 从 Firebase 获取特定数据

[英]How to get specific data from Firebase with Kotlin

So I have a movie app.所以我有一个电影应用程序。 If I liked the movie I'm gonna save its id to firebase.如果我喜欢这部电影,我会将其 id 保存到 firebase。 And my goal is to get all movie_id's and display them on screen.我的目标是获取所有movie_id 并将它们显示在屏幕上。

This is the saving code.这是保存代码。

firebaseDb.getReference("Users").child(uid).child("movie")
            .push().child("movie_id")
            .setValue(args.movie.id)

And this is the firebase display这是firebase显示器

火力基地

So how to get all movie_id's?那么如何获取所有movie_id呢? I tried this but nothing happened.我试过了,但什么也没发生。

firebaseDb.getReference("Users").child(uid).child("movie").addValueEventListener(object :
ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
    val map: Map<String, Any> = snapshot.getValue() as Map<String, Any>

    val mapSize = map.keys.size
    for (i in 1..mapSize) {
        val keys = map.filterKeys { it == map.keys.toTypedArray()[i - 1] }       
    }

}

If want to create a list of movie IDs, then use the following lines of code:如果要创建电影 ID 列表,请使用以下代码行:

val uid = FirebaseAuth.getInstance().currentUser?.uid
val db = FirebaseDatabase.getInstance().reference
val movieRef = db.child("Users").child(uid).child("movie")
val valueEventListener = object : ValueEventListener {
    override fun onDataChange(dataSnapshot: DataSnapshot) {
        val movieIds = mutableListOf<String>()
        for (ds in dataSnapshot.children) {
            val movieId = ds.child("movie_id").getValue(String::class.java)
            movieIds.add(movieId)
        }
        Log.d("TAG", movieIds.size.toString())
    }

    override fun onCancelled(error: DatabaseError) {
        Log.d("TAG", error.getMessage()) //Never ignore potential errors!
    }
}
movieRef.addListenerForSingleValueEvent(valueEventListener)

The result in the logcat will be: logcat 中的结果将是:

2

As I only see two movies in the screenshot.因为我在屏幕截图中只看到两部电影。

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

相关问题 如何在 Firebase (Kotlin) 中获取特定数据 - How can I get a specific data in Firebase (Kotlin) 如何从 firebase 中的集合中的所有文档中获取特定数据? - How to get specific data from all documents from a collection in firebase? 如何从 firebase 数据快照中获取特定的 snapkey - How do I get a specific snapkey from firebase data snapshot 从特定位置从 firebase 获取数据 - Get data from firebase from a specific position 在 Kotlin 中从 Firebase Firestore 获取一个 Object - Get an Object from Firebase Firestore in Kotlin 使用 Android Kotlin 从 Firebase 获取数据 - fetching data from Firebase with Android Kotlin 在 java/Kotlin/Android 中基于 TimeStamp 从 firestore 中获取数据 || 获取java中的TimeStamp与firebase中的TimeStamp进行比较 - Fetch data from firestore based on TimeStamp in java/Kotlin/Android || Get TimeStamp in java to compare with firebase TimeStamp 如何在android studio-java的firebase数据库中搜索得到子节点的具体数据? - How to get specific data of child node by searching from firebase database in android studio - java? 如何从 firebase 获取最新的更新数据? - How to get the latest updated data from firebase? 如何从 firebase 中的引用文档中获取数据? - How to get data from a referenced document in firebase?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM