2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Firebase Realtime Database (Kotlin) で子の数だけ繰り返し処理したいとき

Last updated at Posted at 2018-12-31

#準備
https://qiita.com/Natsuki_on_Rails/items/c3d76b3fa2048f214aac

#実装

// ...

//①Firebase Databaseのインスタンスを取得
val database = FirebaseDatabase.getInstance()
//②リファレンスを取得
val myRef = database.getReference("hoge")
//③データを取得するお決まりのやつ(リスナーを用意して二つのメソッドをオーバーライド)
myRef.addValueEventListener(object : ValueEventListener {
    override fun onDataChange(dataSnapshot: DataSnapshot) {
        //④dataSnapshotに子供がいると↓こんな感じでループできる
        for (h in dataSnapshot.children){
            val value = h.getValue(Int::class.java)
        }
    }
    override fun onCancelled(error: DatabaseError) {
        // ...
    }
}

こんなかんじで書けば子供の数だけ処理できる。

2
4
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?