Home » Android » Storing the same Strings on firebase like string1, string2, string3

Storing the same Strings on firebase like string1, string2, string3

Posted by: admin November 1, 2017 Leave a comment

Questions:

How can I store equal strings os firebase ?

I have a class on firebase that allows me to add students but I don’t know how do I create student1, student2, student3.

This is the code:

 bSave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!validateForm()) {
                return;
            }
            if (mAuth.getCurrentUser() != null) {
                String user_id = mAuth.getCurrentUser().getUid();
                final DatabaseReference add_student_db = FirebaseDatabase.getInstance().getReference().child("Users").child(user_id).child("Alunos");
                final String anome = aNome.getText().toString();
                Map newPost = new HashMap();
                newPost.put("Aluno", anome);
                add_student_db.setValue(newPost);
            }else {
                Toast.makeText(AddStudent.this, "Erro ao Adicionar o Nome",
                        Toast.LENGTH_SHORT).show();
            }
        }
    });

“Aluno” is student in portuguese.

My child stops at “Alunos” and I want it to create inside of “Alunos”, “Aluno1”, “Aluno2″…

Thanks.

Update:

I solved my problem by doing this:

 if (mAuth.getCurrentUser() != null) {
                String user_id = mAuth.getCurrentUser().getUid();
                final String anome = aNome.getText().toString();
                if (filePath != null){
                    final StorageReference add_student_sr = FirebaseStorage.getInstance().getReference().child("Users").child(user_id).child("Alunos").child(anome);
                    StorageReference riversRef = add_student_sr.child(anome+"_profilePic.jpg");
                    riversRef.putFile(filePath)
                            .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                                @Override
                                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                                    Toast.makeText(getApplicationContext(), "File Uploaded ", Toast.LENGTH_LONG).show();
                                    String downloadUrl = taskSnapshot.getDownloadUrl().toString();
                                    String user_id = mAuth.getCurrentUser().getUid();
                                    final DatabaseReference add_student_db = FirebaseDatabase.getInstance().getReference().child("Users").child(user_id).child("Alunos").child(anome);
                                    Map newPost = new HashMap();
                                    newPost.put("profilePic", downloadUrl);
                                    add_student_db.setValue(newPost);
                                }
                            })
                            .addOnFailureListener(new OnFailureListener() {
                                @Override
                                public void onFailure(@NonNull Exception exception) {

                                    Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
                                }
                            })
                            .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                                @Override
                                public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                                    double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();

                                }
                            });
                }
Answers:

One way to handle this is to call push().

When you call push() Firebase creates a new unique location under the location. You can then call setValue on that new location

final DatabaseReference add_student_db = FirebaseDatabase.getInstance().getReference().child("Users").child(user_id).child("Alunos");
final String anome = aNome.getText().toString();
add_student_db.push().setValue(anome);

Every time you run this code a new child node will be created under /Users/$user_id/Alunos with "Aluno": "Name from aNome".