【问题标题】:How do I retrieve a list of strings from firebase database containing child names?如何从包含子名称的 firebase 数据库中检索字符串列表?
【发布时间】:2018-11-04 15:26:03
【问题描述】:

我正在使用 Kotlin 开发一个应用,并且已经设置了我的 Firebase 数据库。我能够将数据写入我的数据库,但是,我无法从中读取数据。这是我的数据库的图像:

我想检索BasicsFunctionsLoops 中所有标题的列表。有关更多上下文,这是一个教人们如何在 Kotlin 中编码的应用程序,在进度选项卡下,我基本上列出了用户在我的应用程序中完成的所有主题。那么,如何查询我的数据库以检索 Basics 下列出的所有标题的列表?我希望最好是包含字符串ConditionDatatype 的字符串列表。我只需要他们为该特定部分完成的主题的名称(在本例中为Basics)。所以基本上,我需要BasicsFunctionsLoops 的子名。

我的进步课程:

package nus.is3261.kotlinapp
import com.google.firebase.database.IgnoreExtraProperties

@IgnoreExtraProperties
class Progress {
    var completed: String? = null

    constructor() {
        // Default constructor required for calls to DataSnapshot.getValue(Post.class)
    }

    constructor(time: String) {
        this.completed = time
    }

}

我如何将数据写入我的数据库:

/**
 * For firebase database
 */
private fun submitProgress(str: String, chapter: Int) {
    val currentUser = mAuth!!.currentUser
    // if user is not logged in, don't write
    currentUser?:return
    val time = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().time)
    val progress = Progress(time)
    when(chapter){
        1 -> mProgressReference!!.child(currentUser.uid!!).child("Progress").child("Basics").child(str).setValue(progress)
        2 -> mProgressReference!!.child(currentUser.uid!!).child("Progress").child("Loops").child(str).setValue(progress)
        3 -> mProgressReference!!.child(currentUser.uid!!).child("Progress").child("Functions").child(str).setValue(progress)
    }
}

【问题讨论】:

  • 为了更好地理解,您只想显示:BasicsFunctionsLoops,它们存在于Progress 节点下?
  • @AlexMamo 嗨,我需要BasicsFunctionsLoops 下所有孩子的姓名。我打算有一个Chart 部分,显示已完成的主题。因此,我打算查询数据库以获取所有已完成主题的名称。
  • 好的,那么预期的结果是什么?您要显示的确切值是什么?
  • @AlexMamo 所以预期是,我查询我的数据库并获取所有已完成主题的字符串列表,这些主题是:ConditionData TypeRecursionWhenWhile .然后我使用这些数据绘制图表,告诉用户他们已完成和尚未完成的章节。
  • 我明白了,我马上给你写答案。

标签: android firebase android-studio firebase-realtime-database kotlin


【解决方案1】:

根据您的评论,要显示BasicsFunctionsLoops 下的孩子,请使用以下代码:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference progressRef = rootRef.child("Users").child(uid).child("Progress");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot dSnapshot : dataSnapshot.getChildren()) {
            for(DataSnapshot ds : dSnapshot.getChildren()) {
                String key = ds.getKey();
                Log.d(TAG, key);
            }
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage());
    }
};
progressRef.addListenerForSingleValueEvent(valueEventListener);

您的 logcat 中的结果将是:

Condition
Data Type
Recursion
When
While

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    • 2016-02-03
    • 1970-01-01
    相关资源
    最近更新 更多