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