Search code examples
androidfirebasefirebase-realtime-databasearduino-esp8266

Save button click event of Android app in Firebase


My app is connected but not getting updated as in this picture you can see I'm a newbie in Android application development. I want to save and retrieve my button click events in Firebase. My Android code for button click is:

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

Can I add the v.getID for the "database reference" and "set value" to onoff (variable)? If not, then how can I add the button click data? Also, how to retrieve the data through "data change" event.

Please help.


Solution

  • enter image description hereMaintain each Button Name's in the Firebase Database as nodes and maintain a child named as "clicked" under each Button-node as a boolean variable 'true' for ON and 'false' for OFF.and start updating those nodes based on your switch position.

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