How to create a class to connect to in Firebase?

Asked

Viewed 58 times

1

I’m trying to create a class where I’ll connect in the Firestore of Firebase, and then I’ll take this class and instantiate it where necessary, I made this part of the code but I don’t know what else to do.

Class connecting in firebase

public class Cloudfirestore {

FirebaseFirestore connection;

public CloudFirestore(FirebaseFirestore connection) {
    this.connection = connection;
}

public FirebaseFirestore getConnection() {
    return connection;
}

public void setConnection(FirebaseFirestore connection) {
    this.connection = connection;
}

Class that prompts my connection and adds a record in firebase.

Cloudfirestore cloudFirestore = new Cloudfirestore();

    Map<String, Object> report = new HashMap<>();
    report.put("name", this.editTextName);
    report.put("dateWorked", this.editDateWorked);
    report.put("timeInput", this.editTimeInput);
    report.put("timeExit", this.editTimeExit);
    report.put("goodParts", this.editNumberGoodParts);
    report.put("badParts", this.editNumberBadParts);
    report.put("workedPlace", this.editTextWorkedPlace);
    report.put("mails", this.editTextMails);

    if (report != null) {

        cloudFirestore.setConnection().collection("reports")
                .add(report)
                .addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
                    @Override
                    public void onSuccess(DocumentReference documentReference) {
                        Log.d(TAG, "DocumentSnapshot added with ID: " + documentReference.getId());
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.w(TAG, "Error adding document", e);
                    }
                });

1 answer

0

At first glance, I just miss the constructor to boot the class...

Something like: public CloudFirestore () { FirebaseFirestore db = FirebaseFirestore.getInstance(); }

Is the class giving any error? It is difficult to help without knowing the problem...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.