使用每个循环检查Firebase实时数据库的类模型中是否存在数据

时间:2019-01-17 15:33:06

标签: android firebase firebase-realtime-database

我正在尝试显示Firebase实时数据库中的文件列表,但我想检查数据库中是否不存在文件,因此我可以显示带有文本的textView,以向用户指示数据库中尚无文件。

我尝试显示文件,但是当数据库中不存在文件时,textView不显示含义是代码中做错了什么。下面是我尝试过的方法,但是它不起作用。请问我在做错什么吗?

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();
        }
    });

}

1 个答案:

答案 0 :(得分:3)

要检查您的参考书中是否存在某些内容,可以使用

 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
               }

要检查该引用是否也有0个以上的子级,可以使用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
                   }