0

I have been struggling with this for a while, and I simply cannot understand where I am going wrong. I've tried multiple ways - lately adding my own listeners but after that following a somewhat recent tutorial on sending data to firebase, but for some reason I keep running into a similar issue every time -- ie. to me it seems like the setValue doesn't... work?

I'm quite sure that it is something to do with my setup - but I cannot figure out what it is and hopefully someone can help me out.

Latest try is below. I know the listeners rely on toasts which is less than ideal - but none show up at the moment.

Thanks for any help in advance

        package com.example.firebase_rb;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

    public class MainActivity extends AppCompatActivity {

    DatabaseReference databaseReference;

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

        databaseReference= FirebaseDatabase.getInstance().getReference("this is the path");

        databaseReference.setValue("here there").addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void unused) {
                Toast.makeText(getApplicationContext(), "success", Toast.LENGTH_SHORT).show();
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(getApplicationContext(), "failed", Toast.LENGTH_SHORT).show();
            }
        }).addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                Toast.makeText(MainActivity.this, "wtf", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
copyleft
  • 39
  • 6
  • If you already know that toasts are not ideal, consider replacing them with `Log.i` statements and including the updated code and its output in the question. – Frank van Puffelen Mar 31 '22 at 20:51
  • Recently when this happens it mostly is that folks created the database in the Firebase console *after* they downloaded the `google-services.json` and thus the URL is missing from there. If that is the cause for you too, you can fix it most easily by specifying the database URL in your code: `FirebaseDatabase.getInstance("database URL here").getReference(...)` – Frank van Puffelen Mar 31 '22 at 20:52
  • I went through the Firebase setup that android studio offers - but Franks addition did the trick! I will have to go through the app I build earlier to see where this ties into it, but now for the first time data did go through :) Thanks a ton. – copyleft Mar 31 '22 at 21:21
  • If you want to perform some CRUD operations in the Realtime Database, then I think that this [article](https://medium.com/firebase-tips-tricks/how-to-make-a-clean-architecture-android-app-using-mvvm-firestore-and-jetpack-compose-abdb5e02a2d8) might help. – Alex Mamo Apr 04 '22 at 08:17

0 Answers0