簡體   English   中英

如何在實時數據庫中檢查值(打開/關閉)

[英]How to check value in realtime database (ON/OFF)

我如何在這段代碼中

    @Override
protected void onStart() {
    super.onStart();
    //if the user is already signed in
    //we will close this activity
    //and take the user to profile activity
    if (mAuth.getCurrentUser() != null) {
        finish();
        startActivity(new Intent(this, ActivitySplash.class));
    }

}

請檢查子項(userId)是否設置為ON / OFF,如果為ON,則運行代碼

    if (mAuth.getCurrentUser() != null) {
    finish();
    startActivity(new Intent(this, ActivitySplash.class));
}

如果關閉,則顯示特定活動。

我的資料庫 我的數據庫結構

正如@FrankvanPuffelen所說,您應該花一些時間閱讀文檔,這將有助於您自己編寫代碼,不過我還是在這里為您介紹這些內容。 它應該使事情更清楚。

通過從數據庫正確引用所需的節點,然后使用正確的eventListener來完成從數據庫的eventListener 存在三種eventListenerssingleValueEventListenervalueEventListenerchildEventListener

詳細了解每個文檔。 此答案還可以幫助您了解childEventListeners

要檢索status節點的值,您必須依次遍歷數據庫父節點,即users和具有值nfe... uid

所以在代碼中看起來像這樣:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users").child(uid);

// uid has the value nfe...


ref.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String status = dataSnapshot.child("status").getValue(String.class);
             // compare the value of status here and do what you want    
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.d(TAG, "onCancelled", databaseError.toException());
            }


        });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM