Firebase 快照不存在

问题描述 投票:0回答:1
Log.d("ıd check",subCatId+catId);
database.getReference().child("categories").child(catId)
.child("subCategories").child(subCatId)
.child("questions").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        if (snapshot.exists()) {
            Log.d("exixtance","DOES EXİSTS");
            timer.start();
            for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
                prossesdata(dataSnapshot);
                loadingdialog.dismiss();
            }
            if (list.size() > 0) {
                for (int i = 0; i < 4; i++) {
                    binding.optioncontainer.getChildAt(i).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            checkAnswer((Button) v);
                        }
                    });
                }
                playAnimation(binding.question, 0, list.get(position).getQuestion());

                binding.buttonnext.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        enableOptions(true);
                        position++;
                        if (position == list.size()) {
                            timer.cancel();
                            Intent intent = new Intent(QuestionActivity.this, ScoreActivity.class);
                            long totaltime = questionTime * 60 * 100;
                            intent.putExtra("timetaken", totaltime - timeLeft);
                            intent.putExtra("correct", correctAnswer);
                            intent.putExtra("wrong", wrongAnswer);
                            intent.putExtra("totalquestion", list.size());
                            startActivity(intent);
                            finish();
                            return;
                        }
                        count = 0;
                        playAnimation(binding.question, 0, list.get(position).getQuestion());
                    }
                });

                binding.buttonsubmit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(QuestionActivity.this, ScoreActivity.class);
                        long totaltime = questionTime * 60 * 100;
                        intent.putExtra("timetaken", totaltime - timeLeft);
                        intent.putExtra("correct", correctAnswer);
                        intent.putExtra("wrong", wrongAnswer);
                        intent.putExtra("totalquestion", list.size());
                        startActivity(intent);
                    }
                });
            }
        } else {
            Toast.makeText(QuestionActivity.this, ""+list.size(), Toast.LENGTH_SHORT).show();
            loadingdialog.dismiss();
        }
    }

我试图从数据库中获取问题,但我的代码只执行 else 部分并跳过第一部分。我不明白为什么。

我怎样才能解决这个问题并使第一部分工作?快照不存在,因此我无法填写 if 条件。

我添加了日志,这是输出:

2024-10-06 17:39:26.060 23619-23619 我会检查 com.example.quiztime D -O8IX4259O4p5Ko_uh4e-O8GtSPIwhXwbPfYy0Bc

这是我的实时数据库:

类别图片 : “https://firebasestorage.googleapis.com/v0/b/quiztimeadminapp.appspot.com/o/categoryImage%2F1727947128858?alt=media&token=320ab53c-20c4-44a9-8233-c8021429b138” 类别名称 : “阿塔波特” 子类别 -O8XHDcdHD1t-E5gI0vR 类别名称 : “子” 问题 -O8XHGRKrlXGoVhREm8I 正确答案 : “乙” 选项A : “一个” 选项B : “乙” 选项C : “C” 选项D : “D” 问题 : “索鲁1” -O8GfSBC8qjD0D-_7SNL 类别图片 : “https://firebasestorage.googleapis.com/v0/b/quiztimeadminapp.appspot.com/o/categoryImage%2F1727947196927?alt=media&token=b11e44c8-69f4-4889-9269-18c9421d7b6d” 类别名称 : “金枪鱼” -O8GtSPIwhXwbPfYy0Bc 类别图片 : “https://firebasestorage.googleapis.com/v0/b/quiztimeadminapp.appspot.com/o/categoryImage%2F1727950867993?alt=media&token=312244da-1e8e-4188-8f81-b7b81d056125” 类别名称 : “阿尔曼卡” 子类别

数据库位置:比利时(europe-west1)

android firebase firebase-realtime-database
1个回答
0
投票
Log.d("ID Check", "catId: " + catId + " subCatId: " + subCatId);

database.getReference()
.child("categories")
.child(catId)
.child("subCategories")
.child(subCatId)
.child("questions")
.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        if (snapshot.exists()) {
            // Process the data
            Log.d("Existence", "Data exists in the path.");
        } else {
            Log.d("Existence", "Snapshot does not exist");
        }
    }
    
    @Override
    public void onCancelled(@NonNull DatabaseError error) {
        Log.e("FirebaseError", error.getMessage());
    }
});

查看规则

{
  "rules": {
    "categories": {
      "$catId": {
        "subCategories": {
          "$subCatId": {
            "questions": {
              ".read": true
            }
          }
        }
      }
    }
  }
}

调试日志

Log.d("ID Check", "Category ID: " + catId + ", Subcategory ID: " + subCatId);
© www.soinside.com 2019 - 2024. All rights reserved.