Home > Enterprise >  Why My Firebase database not storing data according to Model class?
Why My Firebase database not storing data according to Model class?

Time:09-11

I have a fragment which is used to upload pdf files in firebase , I want it to store filename , url along with the username and enrollment of user.

I have an Node with name resume-uploads in which I want to store -

  1. file - name
  2. url
  3. name of the user who uploaded it
  4. enrollment no. of the user

but resume-uploads is only storing file-name and url please help me find my mistake

node in firebase

resume-upload-Fragment

public class ResumeUploadFragment extends Fragment {

    EditText editPDFName;
    Button uploadButton;
    private String username;
    private String enrollment;
    private final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    private final DatabaseReference ref = FirebaseDatabase.getInstance().getReference("datauser").child(user.getUid());
    ActivityResultLauncher<Intent> resultLauncher;
    StorageReference storageReference;
    DatabaseReference databaseReference;


    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_resume_upload, container, false);
        editPDFName = view.findViewById(R.id.edit_text_design);
        uploadButton = view.findViewById(R.id.upload_button);

        storageReference = FirebaseStorage.getInstance().getReference();
        databaseReference = FirebaseDatabase.getInstance().getReference("resume-uploads");


        //initialize resultLauncher
        resultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
                new ActivityResultCallback<ActivityResult>() {
                    @Override
                    public void onActivityResult(ActivityResult result) {
                        if (result.getResultCode() == Activity.RESULT_OK) {
                            // permission granted
                            Intent data = result.getData();
                            Uri uri = data.getData();
                            uploadPDFFile(data.getData());

                        } else {
                            // permision denied
                        }
                    }
                }
        );

        uploadButton.setOnClickListener(new View.OnClickListener() {
         ...


        return view;
    }

    private void uploadPDFFile(Uri data) {
        final ProgressDialog progressDialog = new ProgressDialog(getActivity());
        progressDialog.setTitle("uploading...");
        progressDialog.show();
        StorageReference reference = storageReference.child("resumeUploads/"   System.currentTimeMillis()   ".pdf");
        reference.putFile(data).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                Task<Uri> uri = taskSnapshot.getStorage().getDownloadUrl();
                while (!uri.isComplete()) ;
                Uri url = uri.getResult();

                // getting name and enrollment of current user 

                setUsernameAndEnrollment();
                UploadResumeFile uploadResumeFile = new UploadResumeFile(editPDFName.getText().toString(), url.toString(), username, enrollment);
                if (FirebaseAuth.getInstance().getCurrentUser() != null)
                    databaseReference.child(databaseReference.push().getKey()).setValue(uploadResumeFile);
                Toast.makeText(getActivity(), "File Uploaded Successfully", Toast.LENGTH_SHORT).show();
                progressDialog.dismiss();
            }
        }).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onProgress(@NonNull UploadTask.TaskSnapshot snapshot) {
//                    double progress = (100.0 * snapshot.getBytesTransferred())/snapshot.getTotalByteCount();
                progressDialog.setMessage("Uploading...");
            }
        });
    }

    private void setUsernameAndEnrollment() {

        ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                UserDataStore userDataStore = snapshot.getValue(UserDataStore.class);
                {
                    if (user != null && userDataStore != null) {
                        username = userDataStore.getName();
                        enrollment = userDataStore.getEnrollment();
                    }
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });
    }


    private void openFileDialog(View view) {
        ...
    }

}

**Model Class **

  public class UploadResumeFile {
  public String name ;
  public String url;
  public String username;
  public String enrollment;
  public UploadResumeFile()
  {

  }

    public UploadResumeFile(String name, String url, String username,String enrollment) {
        this.name = name;
        this.url = url;
        this.username = username;
        this.enrollment = enrollment;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUser_name() {
        return username;
    }

    public void setUser_name(String user_name) {
        this.username = user_name;
    }

    public String getEnrollment() {
        return enrollment;
    }

    public void setEnrollment(String enrollment) {
        this.enrollment = enrollment;
    }
}

user model class

package com.project.PlacementInfo.models;

public class UserDataStore {
    String name , email, phone_number, enrollment , branch , CGPA, password ;

    public UserDataStore() {
    }

    public UserDataStore(String name, String email, String phone_number, String enrollment, String branch, String CGPA, String password) {
        this.name = name;
        this.email = email;
        this.phone_number = phone_number;
        this.enrollment = enrollment;
        this.branch = branch;
        this.CGPA = CGPA;
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone_number() {
        return phone_number;
    }

    public void setPhone_number(String phone_number) {
        this.phone_number = phone_number;
    }

    public String getEnrollment() {
        return enrollment;
    }

    public void setEnrollment(String enrollment) {
        this.enrollment = enrollment;
    }

    public String getBranch() {
        return branch;
    }

    public void setBranch(String branch) {
        this.branch = branch;
    }

    public String getCGPA() {
        return CGPA;
    }

    public void setCGPA(String CGPA) {
        this.CGPA = CGPA;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

CodePudding user response:

The issue has been resolved

I was calling the setUsernameandenrollment() method inside uploadPDFfile() ,but I should not call it there.

Now I am calling it in OnCreateView and it is working properly.

  • Related