0

My goal is to not use Firebase, instead use a json file locally and fetch data from it just like I would by using Firebases' DataSnapshot functions: getChildren() and getValue().

I've been following a youtube tutorial on how to create a Quiz application on Android, here is the json that I'm using on Firebase:

{
  "Chapters" : {
    "chapter101" : {
      "name" : "Quiz Chapter 1",
      "sets" : 2
    },
    "chapter102" : {
      "name" : "Quiz Chapter 2",
      "sets" : 1
    },
  },
  "SETS" : {
    "chapter101" : {
      "questions" : {
        "question1" : {
          "correctAnswer" : "A",
          "optionA" : "A",
          "optionB" : "B",
          "optionC" : "C",
          "optionD" : "D",
          "question" : "Lorem ipsum...? (Question 1 from set number 1)",
          "setNo" : 1
        },
        "question2" : {
          "correctAnswer" : "B",
          "optionA" : "A",
          "optionB" : "B",
          "optionC" : "C",
          "optionD" : "D",
          "question" : "Lorem ipsum...? (Question 2 from set number 1)",
          "setNo" : 1
        },
        "question3" : {
          "correctAnswer" : "A",
          "optionA" : "A",
          "optionB" : "B",
          "optionC" : "C",
          "optionD" : "D",
          "question" : "Lorem ipsum...? (Question 1 from set number 2)",
          "setNo" : 2
        },
        "question4" : {
          "correctAnswer" : "B",
          "optionA" : "A",
          "optionB" : "B",
          "optionC" : "C",
          "optionD" : "D",
          "question" : "Lorem ipsum...? (Question 2 from set number 2)",
          "setNo" : 2
        }
      }
    },
    "chapter102" : {
      "questions" : {
        "question1" : {
          "correctAnswer" : "A",
          "optionA" : "A",
          "optionB" : "B",
          "optionC" : "C",
          "optionD" : "D",
          "question" : "Lorem ipsum...? (Question 1 from set number 1)",
          "setNo" : 1
        },
        "question2" : {
          "correctAnswer" : "B",
          "optionA" : "A",
          "optionB" : "B",
          "optionC" : "C",
          "optionD" : "D",
          "question" : "Lorem ipsum...? (Question 2 from set number 1)",
          "setNo" : 1
        }
      }
    }
  }
}

And this is how I am using it in my Java code:

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference();

private List<QuestionModel> list;

private String chapter;
private int setNo;

chapter = getIntent().getStringExtra("chapter");
setNo = getIntent().getIntExtra("setNo");

list = new ArrayList<>();

myRef.child("SETS").child(chapter).child("questions").orderByChild("setNo").equalTo(setNo).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
                    list.add(dataSnapshot.getValue(QuestionModel.class));
                }
                
                if (list.size() > 0) {
                    for (int i = 0; i < 4; i++) {
                        optionsContainer.getChildAt(i).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Button correctOption = (Button) optionsContainer.findViewWithTag(list.get(position).getCorrectANS());
                                if (correctOption != null) {
                                    checkAnswer((Button) v);
                                }
                            }
                        });
                    }
                    playAnim(question, 0, list.get(position).getQuestion());
                    nextBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            nextBtn.setEnabled(false);
                            nextBtn.setAlpha(0.7f);
                            enableOption(true);
                            position++;
                            if (position == list.size()) {
                                Intent scoreIntent = new Intent(QuestionsActivity.this, ScoreActivity.class);
                                scoreIntent.putExtra("score", score);
                                scoreIntent.putExtra("total", list.size());
                                startActivity(scoreIntent);
                                finish();
                                return;
                            }
                            count = 0;
                            playAnim(question, 0, list.get(position).getQuestion());
                        }
                    });
                } else {
                    finish();
                    Toast.makeText(QuestionsActivity.this, "-", Toast.LENGTH_SHORT).show();
                }
                loadingDialog.dismiss();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                Toast.makeText(QuestionsActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
                loadingDialog.dismiss();
                finish();
            }
        });

QuestionModel.class contains these (along with getters):

public class QuestionModel {
private String question, optionA, optionB, optionC, optionD, correctANS;
private int setNo;

public QuestionModel() {
 
}

public QuestionModel(String question, String optionA, String optionB, String optionC, String optionD, String correctANS, int setNo) {
    this.setNo = setNo;
    this.question = question;
    this.optionA = optionA;
    this.optionB = optionB;
    this.optionC = optionC;
    this.optionD = optionD;
    this.correctANS = correctANS;
}

1 Answers1

0

There's no existing equivalent for Firebase's DataSnapshot getChildren function. So, you will have to create one yourself by parsing manually the json response.

Instead of getValue, after getting a response from your server in json format you can convert with Gson library to a model class for easier use. But, you must create this model class and have its contents match those of the json response. More information on how to do this you can find in this thread's answer.

Thanasis M
  • 1,144
  • 7
  • 22