0

我的应用程序已连接但未更新,如图所示,您可以看到我是 Android 应用程序开发的新手。我想在 Firebase 中保存和检索我的按钮单击事件。我的按钮点击Android代码是:

View.OnClickListener btnOnOffClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String onoff="";

           switch(v.getId()){
                case R.id.living_btn:
                    if (click ==0){
                        click=1;
                        onoff = "/SWITCH-01=ON";
                        living_btn.setBackgroundResource(R.drawable.livingroom);
                    }else{
                        click=0;
                        onoff = "/SWITCH-01=OFF";
                        living_btn.setBackgroundResource(R.drawable.livingroom_off);
                    }
                    break;
                case R.id.kitchen_btn:
                    if (click ==0){
                        click=1;
                        onoff = "/SWITCH-02=ON";
                        kitchen_btn.setBackgroundResource(R.drawable.kitchen);
                    }else{
                        click=0;
                        onoff = "/SWITCH-02=OFF";
                        kitchen_btn.setBackgroundResource(R.drawable.kitchen_off);
                    }
                    break;
               case R.id.bedroom1_btn:
                   if (click ==0){
                       click=1;
                       onoff = "/SWITCH-03=ON";
                       bedroom1_btn.setBackgroundResource(R.drawable.bed_room);
                   }else{
                       click=0;
                       onoff = "/SWITCH-03=OFF";
                       bedroom1_btn.setBackgroundResource(R.drawable.bed_room_off);
                   }
                   break;
               case R.id.bedroom2_btn:
                   if (click ==0){
                       click=1;
                       onoff = "/SWITCH-04=ON";
                       bedroom2_btn.setBackgroundResource(R.drawable.bedroom2);
                   }else{
                       click=0;
                       onoff = "/SWITCH-04=OFF";
                       bedroom2_btn.setBackgroundResource(R.drawable.bedroom2_off);
                   }
                   break;
               case R.id.bathroom_btn:
                   if (click ==0){
                       click=1;
                       onoff = "/SWITCH-05=ON";
                       bathroom_btn.setBackgroundResource(R.drawable.bathroom);
                   }else{
                       click=0;
                       onoff = "/SWITCH-05=OFF";
                       bathroom_btn.setBackgroundResource(R.drawable.bathroom_off);
                   }
                   break;
               case R.id.toilet_btn:
                   if (click ==0){
                       click=1;
                       onoff = "/SWITCH-06=ON";
                       toilet_btn.setBackgroundResource(R.drawable.toi_on);
                   }else{
                       click=0;
                       onoff = "/SWITCH-06=OFF";
                       toilet_btn.setBackgroundResource(R.drawable.toi_off);
                   }
                   break;
            }

我可以将v.getID“数据库引用”和“设置值”添加到onoff(变量)吗?如果没有,那么如何添加按钮点击数据?此外,如何通过“数据更改”事件检索数据。

请帮忙。

4

1 回答 1

0

在此处输入图像描述将 Firebase 数据库中的每个 Button Name 维护为节点,并在每个 Button 节点下维护一个名为“clicked”的子节点作为布尔变量“true”表示 ON,“false”表示 OFF。并根据您的开关位置开始更新这些节点.

       Button living_btn = findViewById(R.id.your_btnId);
            Button kitchen_btn  = findViewById(R.id.your_btnId);
            ...
            //declare all your buttonViews...

            //attach the click listener to all buttons u just created like this...

             kitchen_btn.setOnClickListener(btnOnOffClickListener );
            living_btn.setOnClickListener(btnOnOffClickListener );
                //Creating a Reference to Buttons Node in Firebase DB for changing switch positions.
     final FirebaseDatabase database = FirebaseDatabase.getInstance();
                    final DatabaseReference buttons = database.getReference("Buttons");

                 HashMap<String,Object>detailsUpdate = new HashMap<>();
                View.OnClickListener btnOnOffClickListener = new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            String onoff="";

                           switch(v.getId()){
                                case R.id.living_btn:
                                    if (click ==0){
                                        click=1;
                                          detailsUpdate.put("clicked",true);
                             buttons.child("living_btn").updateChildren(detailsUpdate);
                                        onoff = "/SWITCH-01=ON";
                                        living_btn.setBackgroundResource(R.drawable.livingroom);
                                    }else{
                                        click=0;
                                        onoff = "/SWITCH-01=OFF";
                           detailsUpdate.put("clicked",false);
                             buttons.child("living_btn").updateChildren(detailsUpdate);
                                        living_btn.setBackgroundResource(R.drawable.livingroom_off);
                                    }
                                    break;

                               // continue this for everyButton......


           }
//Code for retreiving the button states
  final DatabaseReference ref = database.getReference("Buttons");
ref.child("living_btn").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {
                for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
            String clicked = childSnapshot.getKey();
if(clicked.equalsIgnoreCase("clicked")){
boolean clicked = Boolean.parseBoolean(dataSnapshot.getValue().toString());
//u will get clicked as true or false and then use the info for further coding
}
}
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }


        });
于 2017-01-20T07:14:47.623 回答