0

I am trying to display a list of files from firebase realtime database but i want check if no file exist in database so i can display a textView with a text to indicate to user that no file exist in database yet.

i have tried display the files but when no file exist in database, the textView does not display meaning am doing something wrong in the code. Below is what i tried but it's not working.Please what am i doing wrong?

public void displayDocuments(){

    // display progressbar
    progressBar.setVisibility(View.VISIBLE);

    dBRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            // clear list
            archivedDocumentsList.clear();

            for(DataSnapshot snapshot: dataSnapshot.getChildren()){

                Documents documents = snapshot.getValue(Documents.class);

                if(documents == null){
                    // hides the recyclerView and displays the textView
                    recyclerView.setVisibility(View.GONE);

                    // sets visibility to visible
                    tv_no_archived_document.setVisibility(View.VISIBLE);

                }
                else {
                    // hides the textView and displays the recyclerView
                    tv_no_archived_document.setVisibility(View.GONE);

                    // sets visibility to visible
                    recyclerView.setVisibility(View.VISIBLE);

                    // adds to list
                    archivedDocumentsList.add(archivedDocuments);
                }

            }


            /*if(!dataSnapshot.exists()){
                // hides the recyclerView and displays the textView
                recyclerView.setVisibility(View.GONE);

                // sets visibility to visible
                tv_no_archived_document.setVisibility(View.VISIBLE);

            }
            */

            // notify adapter of changes
            adapterArchivedDocuments.notifyDataSetChanged();

            // hides progressbar
            progressBar.setVisibility(View.GONE);

        }

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

            // hides progressbar
            progressBar.setVisibility(View.GONE);

            // display Error message
            Snackbar.make(constraintLayout,databaseError.getMessage(),Snackbar.LENGTH_LONG).show();
        }
    });

}
ali sampson
  • 321
  • 4
  • 7
  • Please add your database structure and indicate which data you want to check for existens. – Alex Mamo Jan 17 '19 at 15:35
  • Possible duplicate of [How can I check if a value exists already in a Firebase data class Android](https://stackoverflow.com/questions/38948905/how-can-i-check-if-a-value-exists-already-in-a-firebase-data-class-android) – kostas poimenidhs Jan 17 '19 at 22:25
  • @ALex Mamo and Kostas Poime. Thanks for the help – ali sampson Mar 18 '19 at 10:58

1 Answers1

3

To check if something exists at your reference you can use this

 dBRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if(dataSnapshot.exists()){ 
            //Data exists at your reference
              }else{
                //Data does not exists at the reference you are pointing out
               }

To check if that reference also has more than 0 childs you can use getChildrenCount();

dBRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if(dataSnapshot.exists()){ 
                //Data exists at your reference
                  if(dataSnapshot.getChildrenCount() > 0 )
                       //There is 1 or more childs inside that reference
                          else
                          //There is no childs inside that reference

                  }else{
                    //Data does not exists at the reference you are pointing out
                   }
Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77