1

I'm getting started with Firebase, and I followed this simple firebase tutorial here: https://www.youtube.com/watch?v=B1rlT5KQ0yE .

I created a simple activity, that when you press the button "Sunny" it displays sunny, by sending the string sunny to the Firebase database, which then sends it back to my app and fills in a textview that displays sunny. So that is okay.

But, when I check my firebase database, it did not add anything to it.

And, when i write something into my Firebase database, it does not change the textview in real time.

Here is my code for the activity.

public class MainActivity extends AppCompatActivity
{
    private TextView myTextView;
    private Button sunnyButton;
    private Button foggyButton;
    private Firebase mRef;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onStart()
    {
        super.onStart();
        myTextView = (TextView) findViewById(R.id.textViewCondition);
        sunnyButton = (Button) findViewById(R.id.sunnyButton);
        foggyButton = (Button) findViewById(R.id.FoggyButton);

        //Firebase
        mRef = new Firebase("https://example-e04be.firebaseio.com/weather");

        mRef.addValueEventListener(new ValueEventListener()
        {
            //onDatachange will get fired everytime there is a change in your firebase database. CHANCE IN YOUR DATABASE!!!
            @Override
            public void onDataChange(DataSnapshot dataSnapshot)
            {
                String text = dataSnapshot.getValue(String.class);
                myTextView.setText(text);
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });

        sunnyButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                mRef.setValue("sunny");//So "Foggy" string will be sent to our DB. Which will update our DB, then in mRef.addValue...will be called and our textfield will be called. Like a small loop.
            }
        });
    }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
TheQ
  • 1,949
  • 10
  • 38
  • 63
  • There may be issue of permission at firebase database. Have you modified it. If no so you can prefer this [http://stackoverflow.com/a/38199481/5610686](http://stackoverflow.com/a/38199481/5610686). See only permission part here. – Pritish Joshi Jul 09 '16 at 10:34
  • @DevQualwebs Do I need to modify it to do real time changes and save to database. I read the answer in the link you presented, but I'm not sure what i need to change in my "Rules"section on my firebase dashboard? – TheQ Jul 09 '16 at 11:00
  • Yes we need to change the rules of firebase database if we are not doing any kind of firebase authentication. You must see this too [https://firebase.google.com/docs/database/security/](https://firebase.google.com/docs/database/security/). for learning purpose you can set it to public what is shown in the previous link. – Pritish Joshi Jul 09 '16 at 11:10
  • @DevQualwebs Ah I see. Yeah after I changed the authentication in the Rules section of my dashboard it works. Thanks for the tip sir! – TheQ Jul 09 '16 at 11:19

1 Answers1

2

For using firebase real time database just leave this...

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

For storage just leave this...

service firebase.storage {
  match /b/project-[your_project_number].appspot.com/o {
    match /{allPaths=**} {
      allow read, write;
    }
  }
}

It will define that you can use it publicly no need to of any verifaction at firebase end.

Pritish Joshi
  • 2,025
  • 3
  • 18
  • 33
  • @Dev_Qualwebs Hey just a quick question. I see that in my firebase main page for my app that I have the "database" section. And "storage" section. When and how would I use storage. If I wanted to store a string from my user, is it technically stored in my "database" section? Or does it have to be moved to my "storage" section in my Firebase? I hope you can clarify that? – TheQ Jul 11 '16 at 05:27