根据firebase数据更新android UI中的切换开关状态

时间:2018-03-05 03:42:13

标签: android firebase-realtime-database

我最近开始研究android和firebase。我正在尝试为IOT开发应用程序,其中应用程序应从firebase获取交换机的状态(1或0)并更新切换开关状态。 This is the firebase json data. I need to update toggle switches based on the values of LR1, LR2, LR3

我已尝试将ValueEventListener附加到onDataChange()(Bundle savedInstanceState),onDataCancelled()中的切换开关和覆盖onSavedInstanceStateonStart()方法。一旦我杀死应用程序并再次启动,所有开关都显示默认(OFF)状态。我需要UI在每次启动时使用当前值进行更新,即使应用程序处于前台也应该更改。请帮帮我。提前谢谢!!

This is onCreate() method This is onStart() method This is SwitchStatus Class

1 个答案:

答案 0 :(得分:0)

声明您的小部件

private Switch sNotificationIndividualChat;
private DatabaseReference fbDbRefRoot;

在onCreate中将其绑定

sNotificationIndividualChat = findViewById(R.id.a_my_profile_settings_s_notification_individual_chat);
fbDbRefRoot = FirebaseDatabase.getInstance().getReference();

然后声明您的模型类

public class MyProfileSettingsModel {

    private String notification_individual_chat;

    public MyProfileSettingsModel() {
    }

    public MyProfileSettingsModel(String notification_individual_chat) {
        this.notification_individual_chat = notification_individual_chat;
    }

    public String getNotification_individual_chat() {
        return notification_individual_chat;
    }

    public void setNotification_individual_chat(String notification_individual_chat) {
        this.notification_individual_chat = notification_individual_chat;
    }
}

然后从firebase获取值并进行相应更新。使用您自己的引用

fbDbRefRoot.child("user_profile_settings").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

        // Use The Model Class To Get The Data
        final MyProfileSettingsModel myProfileSettingsModel = dataSnapshot.getValue(MyProfileSettingsModel.class);

        String notificationIndividualChat = myProfileSettingsModel.getNotification_individual_chat();

        if (notificationIndividualChat.equals("0")) {
            sNotificationIndividualChat.setChecked(true);

            sNotificationIndividualChat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    fbDbRefRoot.child("user_profile_settings").child(current_user_id)
                            .child("notification_individual_chat").setValue("1");

                }
            });
        }
        if (notificationIndividualChat.equals("1")) {
            sNotificationIndividualChat.setChecked(false);

            sNotificationIndividualChat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    fbDbRefRoot.child("user_profile_settings").child(current_user_id)
                            .child("notification_individual_chat").setValue("0");

                }
            });
        }

    }

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

    }
});