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

我们两清 提交于 2020-01-06 11:17:08

问题


How i can in this code

    @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));
    }

}

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.

My database


回答1:


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.

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.

Read more about each of them in detail in docs. This answer can also help you understand childEventListeners.

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());
            }


        });


来源:https://stackoverflow.com/questions/53355996/how-to-check-value-in-realtime-database-on-off

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!