![](/img/trans.png)
[英]How can I retrieve all nodes data from Firebase to listview in Android?
[英]How can i retrieve values of Firebase Nodes with random auto generated IDs in Android Using Kotlin
為了能夠獲取21-6-21
節點下存在的所有鍵和值,您需要循環遍歷 DatasnaShot 對象,如以下代碼行所示:
val rootRef = FirebaseDatabase.getInstance().reference
val dateRef = rootRef.child("SigninData").child("CSC101").child("21-6-21")
val valueEventListener = object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
for (ds in dataSnapshot.children) {
val key = ds.getkey()
val value = ds.getValue(String::class.java)
Log.d("TAG", "$key/$value")
}
}
override fun onCancelled(databaseError: DatabaseError) {
Log.d("TAG", databaseError.getMessage()) //Don't ignore potential errors!
}
}
dateRef.addListenerForSingleValueEvent(valueEventListener)
logcat 中的結果將是:
-Mf8...ESM/oma@gmail.com...
-Mf8...7nb/oma@gmail.com...
-Mf8...XJv/oma@gmail.com...
編輯:
private fun getDataFrom(date: String) {
val rootRef = FirebaseDatabase.getInstance().reference
val dateRef = rootRef.child("SigninData").child("CSC101").child(date)
val valueEventListener = object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
for (ds in dataSnapshot.children) {
val key = ds.getkey()
val value = ds.getValue(String::class.java)
Log.d("TAG", "$key/$value")
}
}
override fun onCancelled(databaseError: DatabaseError) {
Log.d("TAG", databaseError.getMessage()) //Don't ignore potential errors!
}
}
dateRef.addListenerForSingleValueEvent(valueEventListener)
}
現在您可以使用以下任一方法調用上述方法:
getDataFrom("21-6-21")
要么:
getDataFrom("22-6-21")
如果您需要同時獲取它們,那么您應該使用 get() 調用,並將兩個 Task 對象傳遞給whenAllSuccess(Task...<?> tasks) 。
我相信這對你有用。
fun getDataFromFirebaseDatabase() {
// Your path
database.getReference("SigninData/CSC101/21-6-21")
.get()
.addOnCompleteListener {
//this checks if there were any exceptions or Errors generated and will Log the error in your Logcat.
if (it.exception != null) Log.e("FirebaseDatabase", "Error", it.exception)
// this checks if your result was successful or not and also,
//if it has children or not, if both conditions are satisfied,
//it will enter the for loop and print all the
// values along with their keys in logcat.
if (it.isSuccessful && it.result.hasChildren()) {
for (i in it.result.children) {
//i.key will give you required key
Log.d("FirebaseDatabase", "key is ${i.key}")
// i.value will give respective value
// import the values as given in database, for your case it would be String so.
val data = i.value as String
Log.d("FirebaseDatabase", "value is $data")
}
}
}
}
注意 - 這將讀取 21-6-21 子項中的所有值
您的 Logcat 應該看起來像這樣。
//if there is an error
E/FirebaseDatabase: Error <Some Error Code Here>
//if Run is Successful
//since there are 3 entries in CSC101 node all three will be printed one by one
//like the example given below
D/FirebaseDatabase: key is Mf80wSd9neOAqlPhESM
D/FirebaseDatabase: value is oma@gmail.com You signed in for CSC101
上面的代碼使用get()函數,這意味着您的應用程序只會讀取數據庫一次。 除了 get() 您還可以使用:
都是為了不同的目的。 以下是有關這些的 Firebase 文檔: https ://firebase.google.com/docs/database/android/read-and-write#read_data
第 3 個函數類似於 get 函數,但第 3 個函數將從緩存中讀取,並且應該在您不希望數據頻繁更改時使用。
一旦數據庫節點發生更改,第一個和第二個將從數據庫中獲取結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.