skip to Main Content
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();
        }
    }

I am trying to get the questions from database, but my code just performs the else part and skips the first part. I can’t understand why.

How can I fix this problem and make the first part work? The snapshot doesn’t exist, thus I can’t fill the if condition.

I added the log and this is the ouput:

2024-10-06 17:39:26.060 23619-23619 ıd check com.example.quiztime D -O8IX4259O4p5Ko_uh4e-O8GtSPIwhXwbPfYy0Bc

And this is my realtime database :

{
  "categories": {
    "-O8GtSPIwhXwbPfYy0Bc": {
      "categoryImage": "https://firebasestorage.googleapis.com/v0/b/quiztimeadminapp.appspot.com/o/categoryImage%2F1727950867993?alt=media&token=312244da-1e8e-4188-8f81-b7b81d056125",
      "categoryName": "almanca ",
      "subCategories": {
        "-O8HS_G7EjfqXDUij4El": {
          "categoryName": "a1 almanca",
          "questions": {
            "-O8LtLHmg-9rgchOJiKT": {
              "correctAnswer": "Bukres ",
              "optionA": "Londra ",
              "optionB": "Bukres ",
              "optionC": "Paris ",
              "optionD": "Madrid",
              "question": "ingilterenin baskenti neresidir "
            },
            "-O8WplOEJ9gMN_tW0DjM": {
              "correctAnswer": "Sjsj",
              "optionA": "Sjsj",
              "optionB": "Sjsj",
              "optionC": "Sjjs",
              "optionD": "Sjdj",
              "question": "deneme 2"
            }
          }
        },
        "-O8IX4259O4p5Ko_uh4e": {
          "categoryName": "a2 almanca"
        }
      }
    }
  }
}

2

Answers


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

    Check rules

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

    Debugging logs

    Log.d("ID Check", "Category ID: " + catId + ", Subcategory ID: " + subCatId);
    
    Login or Signup to reply.
  2. Your code reads data from this path:

    /categories/$catId/subCategories/$subCatId/questions
    

    If we look at the JSON you shared, this is the closest path to that:

    {
      "categories": {
        "-O8GtSPIwhXwbPfYy0Bc": {
          "categoryImage": "https://firebasestorage.googleapis.com/v0/b/quiztimeadminapp.appspot.com/o/categoryImage%2F1727950867993?alt=media&token=312244da-1e8e-4188-8f81-b7b81d056125",
          "categoryName": "almanca ",
          "subCategories": {
            "-O8IX4259O4p5Ko_uh4e": {
              "categoryName": "a2 almanca"
            }
          }
        }
      }
    }
    

    But there is no questions node under the -O8IX4259O4p5Ko_uh4e subcategory (only a categoryName). So it is indeed expected that snapshot.exists() returns false in the code you shared, as it reads a node that doesn’t exist in your database.

    If you want to make the code work, add a questions node under subcategory -O8IX4259O4p5Ko_uh4e, similar to the one you have under subcategory -O8HS_G7EjfqXDUij4El.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search