0

I'm trying to figure out how to use Firebase properly, so I made this simple code, saving a "game" object with code and password. I also made it show the game's key on the screen. running the code, it works and shows what looks like a key on the screen, but I can't find any of the data anywhere on my Firebase Console. Here's the code:

public class testthing extends AppCompatActivity implements View.OnClickListener {
    EditText pass;
    TextView showkey;
    EditText code;
    Button create;
    String codestr;
    String passstr;
    FirebaseDatabase firebaseDatabase;
    DatabaseReference gameRef;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_testthing);
        pass = findViewById(R.id.pass);
        code = findViewById(R.id.code);
        create = findViewById(R.id.create);
        showkey = findViewById(R.id.showthingy);
        firebaseDatabase = FirebaseDatabase.getInstance();
        create.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if(view==create){
            codestr=code.getText().toString();
            passstr=pass.getText().toString();

            Game g = new Game(codestr,passstr,"");
            gameRef = firebaseDatabase.getReference("gameRooms").push();
            g.key = gameRef.getKey();
            gameRef.setValue(g);
            showkey.setText(g.key);

        }
    }
}

with the "game" class being:

@IgnoreExtraProperties
public class Game {
    public String key;
    public String code;
    public String password;
    public Game(){
    }
    public Game(String code, String password,String key){

        this.code = code;
        this.password = password;
        this.key = key;
    }

}
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Aviv
  • 35
  • 5

1 Answers1

1

When you're using the following line of code:

gameRef.setValue(g);

The data you're trying to write into the database may succeed or fail, but you'll never know that since you aren't attaching a listener to check that. So solve this, you have to attach a listener as in the following lines of code:

gameRef.setValue(g).addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull Task<Void> task) {
        if (task.isSuccessful()) {
            Log.d("TAG", "The operation is complete.");
            showkey.setText(g.key);
        } else {
            Log.d("TAG", task.getException().getMessage());
        }
    }
});

If the operation fails, most likely the Firebase servers rejected your operation. So make sure you have the proper rules. Otherwise, set the key to the TextView. Remember that all Firebase APIs are asynchronous. This includes the write and the read operations. If you need later to read the data, I recommend you check the following article:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • I'll try that now. What Log.d is? Is it like a toast? – Aviv May 23 '22 at 10:14
  • It helps you log a message or an error message. You can use a Toast as well if you want. Give it a try and tell me if it works. – Alex Mamo May 23 '22 at 10:19
  • I've added a setText to the fail option too and nothing happens. I don't see anything on the Logcat and the showtext TextView still shows the default text – Aviv May 23 '22 at 10:50
  • What is the location of the database? If it's different than the default one, then check this [answer](https://stackoverflow.com/questions/67795058/save-android-user-credentials-to-firebase-database-kotlin/). Be also sure to have an internet connection. Besides that, setText should only be used when the task is successful. – Alex Mamo May 23 '22 at 11:05
  • it is the central european one, thought it makes sense as it's the closest to me geographically. will check linked answer – Aviv May 23 '22 at 11:15
  • Ok, add that URL and try again. Does it work now? – Alex Mamo May 23 '22 at 11:17