Sainita
Sainita

Reputation: 362

Unable to Write to Firebase database to specific node

I am trying to write some data to Firebase Database to the isAdmobEnabled node directly and not to create a child of it

enter image description here

What i am trying to achieve is the following output :

isAdmobEnabled : "true"

But my code is creating a child of it, how to avoid it? This is what i am trying :

adSubmitReference = FirebaseDatabase.getInstance().getReference("Ads").child("isAdmobEnabled");
UpdateAdsModel updateAdsModel = new UpdateAdsModel(switchStatus);
adSubmitReference.setValue(updateAdsModel).addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull Task<Void> task) {
        if (task.isSuccessful()) {
            Toast.makeText(AdsActivity.this, "Task Completed Successfully", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(AdsActivity.this, "Some Error Occurred", Toast.LENGTH_SHORT).show();
        }
    }
});

Here is my UpdateAdsModel class :

public class UpdateAdsModel {

    String isAdmobEnabled;

    public UpdateAdsModel() {
    }

    public UpdateAdsModel(String isAdmobEnabled) {
        this.isAdmobEnabled = isAdmobEnabled;
    }

    public String getIsAdmobEnabled() {
        return isAdmobEnabled;
    }
}

Please help me fix this issue.

Upvotes: 0

Views: 75

Answers (4)

Indrajeet
Indrajeet

Reputation: 1

DatabaseReference Ref=FirebaseDatabase.getInstance().getReference("Ads");
DatabaseReference Ref1 = zonesRef.child("IsAdmobEnabled");
DatabaseReference zone1NameRef = zone1Ref.child("IsAdmobEnabled");

Upvotes: -1

Gulab Sagevadiya
Gulab Sagevadiya

Reputation: 991

adSubmitReference = FirebaseDatabase.getInstance().getReference("Ads").child("isAdmobEnabled");

Just Remove this Child like this

adSubmitReference = FirebaseDatabase.getInstance().getReference("Ads");

Upvotes: 1

LinuxDevil
LinuxDevil

Reputation: 96

You could use:

FirebaseDatabase.getInstance().getReference("Ads").child("isAdmobEnabled").setValue(switchStatus);

Why? You are setting the "isAdmobEnabled" value to an entire object, like this in JSON:

{"isAdmobEnabled": {"isAdmobEnabled": "true"}}.

Upvotes: 1

Frank van Puffelen
Frank van Puffelen

Reputation: 600006

Since your adSubmitReference variable already refers to the isAdmobEnabled property in the database, you should set only the value of that property to it.

So:

adSubmitReference = FirebaseDatabase.getInstance().getReference("Ads").child("isAdmobEnabled");
adSubmitReference.setValue(switchStatus)

If you already have an existing UpdateAdsModel object, you can also read the specific property value from that object like this:

adSubmitReference = FirebaseDatabase.getInstance().getReference("Ads").child("isAdmobEnabled");
adSubmitReference.setValue(updateAdsModel.getIsAdmobEnabled())

Upvotes: 1

Related Questions