Android从Firebase数据库中获取值

时间:2017-11-19 06:20:37

标签: android firebase firebase-realtime-database

你好,所以我现在已经被打了一段时间了。我想咨询一下我的代码。我想申请 我的应用程序的着色功能。

当此人按下按钮时:

  • GREEN说明它将数据库上的值更新为BEING HOUSEKEPT

  • 按下
  • YELLOW状态发送READY FOR INSPECTION RoomStatus

  • 按下
  • RED状态发送READY FOR HOUSEKEEPING RoomStatus

它工作得更早,但是当我试图限制用户是Housekeepers的用户无法访问House Keepers的RED STATE时,我在我想要实现它的代码中插入了某个地方。

我在这里循环,有人可以告诉我哪里做错了吗?

这是我的代码:

        Button room1;
        private DatabaseReference mFirebaseDatabase, mFirebaseDatabase1, mFirebaseDatabase1room;
        private FirebaseDatabase mFirebaseInstance;
        private DatabaseReference referenceroom1;
        private  String roomStat;



        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_navi_to_scan2);
            mFirebaseInstance = FirebaseDatabase.getInstance();
            mFirebaseDatabase1 = mFirebaseInstance.getReference("Rooms");
            mFirebaseDatabase = mFirebaseInstance.getReference("Users");
            mFirebaseDatabase1room = mFirebaseInstance.getReference("Rooms").child("Room1");
            referenceroom1 = mFirebaseDatabase1.child("Room1").child("RoomStatus");

            room1 = (Button) findViewById(R.id.rm1Btn);




            mFirebaseDatabase1.child("Room1").addValueEventListener(new ValueEventListener() { //attach listener

                @Override
                public void onDataChange(DataSnapshot dataSnapshot) { //something changed!


                    for (DataSnapshot locationSnapshot : dataSnapshot.getChildren()) {
                        String location = locationSnapshot.getValue().toString();

                        if (location.equals("READY FOR HOUSEKEEPING")) {
                            room1.setBackgroundColor(Color.GREEN);
                            roomStat = "Green";
                        } else if (location.equals("BEING HOUSEKEPT")) {
                            room1.setBackgroundColor(Color.YELLOW);
                            roomStat = "Yellow";
                        } else {
                            room1.setBackgroundColor(Color.RED);
                            roomStat = "Red";
                        }


                        if (roomStat.equals("Green")) {

                            room1.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    String message = "BEING HOUSEKEPT";
                                    DatabaseReference reference = mFirebaseDatabase1.child("Room1").child("RoomStatus");
                                    reference.setValue(message);
                                    Intent next1 = new Intent(getApplicationContext(), ReaderActivity3.class);
                                    startActivity(next1);
                                }
                            });


                        } else if (roomStat.equals("Yellow")) {
                            room1.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    String message = "READY FOR INSPECTION";
                                    DatabaseReference reference = mFirebaseDatabase1.child("Room1").child("RoomStatus");
                                    reference.setValue(message);
                                    Intent next1 = new Intent(getApplicationContext(), ReaderActivity2.class);
                                    startActivity(next1);




                                }
                            });

                        } else {
                            room1.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    String message = "READY FOR HOUSEKEEPING";
                                    DatabaseReference reference = mFirebaseDatabase1.child("Room1").child("RoomStatus");
                                    reference.setValue(message);
                                    Intent next1 = new Intent(getApplicationContext(), ReaderActivity.class);
                                    startActivity(next1);



                                    //I ALSO WANT TO PUT A CONDITION HERE FETCHING  userType from Structure
                                    // mFirebaseDatabase = mFirebaseInstance.getReference("Users").child("userKey");
                                    //but doing this would mean that I would have to put a listener inside, would that be okay? I tried when this
                                    //was working at first but it didin't and now the whole thing is not working


                                }
                            });

                        }
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) { //update UI here if error occurred.

                }
            });



        }

这是我的结构:

screenshot 1

screenshot 2

screenshot 3

你能帮我确定一下我实现这个问题的原因是什么吗?

enter image description here

1 个答案:

答案 0 :(得分:0)

我没有看错,请尝试调试。 Android现在是有缺陷的,特别是当你连接到云数据库时。在运行按钮的左侧,虫子形成了一个。调试。

或许您可以尝试显示您的location哪个是您的字符串,以检查您是否从数据库中提取了正确的信息。您可以使用日志。可以在这里找到指南

https://developer.android.com/studio/debug/am-logcat.html