How to Update data on Firebase through Android Application?

I tried saving data in Firebase but catches Exception. I am new to Firebase and followed the tutorial on Firebase site where I was also asked to add the file it provides to add in Android Project. Is there any necessity to create a java class for my objects? (I don't think so)

//code in onCreate

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference mDatabase = database.getReference("-KTahsvshhslisAhhjej");

code in onLocationchanged

//I tested Location is received well, i am somewhere messing the syntax
try
{

            mDatabase.child("Alt").setValue(location.getLongitude());
            mDatabase.child("Lat").setValue(location.getLatitude());
            mDatabase.child("Long").setValue(location.getLatitude());
            mDatabase.child("speed").setValue(location.getSpeed());

        }
catch (Exception e)
{
Toast.makeText(getBaseContext(), "Unable to Transfer to Firebase", Toast.LENGTH_LONG).show();
        }

Firebase Database Snap

UPDATED CODE: Still Not getting ans

// onCreate code after editing
FirebaseDatabase database = FirebaseDatabase.getInstance();
            DatabaseReference mDatabase = database.getReference("testing-739c9/-KTJZ7ScdMo0hxKLZr54");


//onLocationChange code after editing
  Map<String,Object> taskMap = new HashMap<String,Object>();
        taskMap.put("Alt", location.getAltitude());
        taskMap.put("Long", location.getLongitude());
        taskMap.put("Lat", location.getLatitude());

    try {
        mDatabase.updateChildren(taskMap);
    } catch (Exception e) {

    try {
        mDatabase.updateChildren(taskMap);

    } catch (Exception e) {

        Toast.makeText(getBaseContext(), "Unable to Transfer to Firebase", Toast.LENGTH_LONG).show();
    }
    }

Upvotes: 3

Views: 2987

Answers (1)

Alex Shutov
Alex Shutov

Reputation: 3282

You have to generate key first by using .push() method, see firebase samples. Use .push().getId() to create new node and give its id. After node is created, you can use .setvalue()

Upvotes: 1

Related Questions