如何在实时数据库中检查值(打开/关闭) [英] 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,则运行代码
make check whether the child (userId) is set to ON / OFF and if ON then we run the code
if (mAuth.getCurrentUser() != null) {
finish();
startActivity(new Intent(this, ActivitySplash.class));
}
如果关闭,则显示特定活动.
if OFF then we show a specific activity.
我的数据库
推荐答案
正如@FrankvanPuffelen所说,您应该花一些时间阅读文档,这将有助于您自己编写代码,但我还是在这里为您简要介绍一下.它应该使事情更清楚.
As @FrankvanPuffelen said, you should spend some time reading docs, it would help you write code yourself, still I am briefing the things for you here. It should make things more clear.
通过从数据库中正确引用所需的节点,然后使用正确的eventListener
来完成从数据库的读取. eventListeners
存在3种类型,singleValueEventListener
,valueEventListener
和childEventListener
.
Reading from database is done by correctly referencing your desired node from the database and then using the correct eventListener
. There are 3 types of eventListeners
present, singleValueEventListener
, valueEventListener
and childEventListener
.
在文档中详细了解它们的更多信息.
此答案还可以帮助您理解childEventListeners
.
Read more about each of them in detail in docs.
This answer can also help you understand childEventListeners
.
要检索status
节点的值,必须依次遍历数据库父节点,这些父节点是users
且值为nfe...
的uid
.
To retrieve the value of status
node you have to go sequentially through your database parent nodes, that are users
and that uid
with value nfe...
.
所以在代码中看起来像这样:
So in code it would look something like this:
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());
}
});
这篇关于如何在实时数据库中检查值(打开/关闭)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!