将Firebase数据快照保存到变量?

时间:2018-10-07 23:51:25

标签: java android firebase firebase-realtime-database

我正在使用android studio创建一个与食物匹配的应用,并且一直在尝试从特定孩子那里检索数据并将其保存到变量中。由于某种原因,它总是返回null。我想知道这是否有可能?我要保存的3个变量在MainActivity部分的顶部声明为字符串变量。我对Firebase还是很陌生,所以我不确定我的语法是否完全正确,但是我之前能够使用相同的语法获取信息。到目前为止,这是我尝试过的两种方法:

private FirebaseAuth auth2 = FirebaseAuth.getInstance();
private String currentUId2 = auth2.getCurrentUser().getUid();
private void getUserFoodChoices() {
    DatabaseReference currentFoodChoices = FirebaseDatabase.getInstance().getReference().child("Users").child(currentUId2);
    currentFoodChoices.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
            String currentUserFoodChoice1 = dataSnapshot.child("foodChoice1").toString();
            String currentUserFoodChoice2 = dataSnapshot.child("foodChoice2").toString();
            String currentUserFoodChoice3 = dataSnapshot.child("foodChoice3").toString();

            //Choice 1 if statements
            if(currentUserFoodChoice1.equals("Burger")){
                currentUserFood1 = "Burger";
            }
            else {
                currentUserFood1 = "Pizza";
            }
            Log.d("Test!:", currentUserFoodChoice1);
            Log.d("Test:", currentUserFood1);
            // "Coffee", "Tea", "Soda", "Hot Chocolate", "Apple Cider"
            //Choice 2 if statements

            if(currentUserFoodChoice2.equals("Coffee")){
                currentUserFood2 = "Coffee";
            }
            else if(currentUserFoodChoice2.equals("Tea")){
                currentUserFood2 = "Tea";
            }
            else if(currentUserFoodChoice2.equals("Soda")){
                currentUserFood2 = "Soda";
            }
            else if(currentUserFoodChoice2.equals("Hot Chocolate")){
                currentUserFood2 = "Hot Chocolate";
            }
            else {
                currentUserFood2 = "Apple Cider";
            }

            //Choice 3 statements

            if(currentUserFoodChoice3.equals("McDonalds")){
                currentUserFood3 = "McDonalds";
            }
            else if(currentUserFoodChoice3.equals("Burger King")){
                currentUserFood3 = "Burger King";
            }
            else if(currentUserFoodChoice3.equals("Dennys")){
                currentUserFood3 = "Dennys";
            }
            else if(currentUserFoodChoice3.equals("UpperCrust")){
                currentUserFood3 = "UpperCrust";
            }
            else if(currentUserFoodChoice3.equals("Arbys")){
                currentUserFood3 = "Arbys";
            }
            else if(currentUserFoodChoice3.equals("Wing City")){
                currentUserFood3 = "Wing City";
            }
            else if(currentUserFoodChoice3.equals("Apple Bees")){
                currentUserFood3 = "Apple Bees";
            }
            else if(currentUserFoodChoice3.equals("Azteca")){
                currentUserFood3 = "Azteca";
            }
            else if(currentUserFoodChoice3.equals("Bob Evans")){
                currentUserFood3 = "Bob Evans";
            }
            else {
                currentUserFood3 = "Wendys";
            }
        }

然后再使用更简洁的代码:

private FirebaseAuth auth2 = FirebaseAuth.getInstance();
private String currentUId2 = auth2.getCurrentUser().getUid();
private void getUserFoodChoices() {
    DatabaseReference currentFoodChoices = FirebaseDatabase.getInstance().getReference().child("Users").child(currentUId2);
    currentFoodChoices.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
            currentUserFood1 = dataSnapshot.child("foodChoice1").toString();
            currentUserFood2 = dataSnapshot.child("foodChoice2").toString();
            currentUserFood3 = dataSnapshot.child("foodChoice3").toString();
        }

        @Override
        public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

        }

        @Override
        public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

}

任何帮助将不胜感激!

0 个答案:

没有答案
相关问题