0

I'm doing a project right now that is supposed to be a math teaching app for kids. I'm using firebase as my database to save the user names and password and also to save the questions and their answers. I have a problem retreiving the question and the answers from the database and display it on the buttons and textview because it appears that the listener for single value event is triggered only when the class is done with all the other commands and i need to use this listener for every question i have.

My database: enter image description here

My code so far:

ArrayList <Integer> list = new ArrayList<Integer>();
    for (int i=1; i<11; i++) {
        list.add(new Integer(i));
    }
    Collections.shuffle(list);

    for (int i=0; i<5; i++) {
        ref2 = ref.child(list.get(i).toString());
        Questions.cont = false;
        getQuestionsFromDb(ref2);

        questionView.setText(Questions.question);
        button1.setText(Questions.ans1);
        button2.setText(Questions.ans2);
        button3.setText(Questions.ans3);
        button4.setText(Questions.ans4);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Questions.cont = true;
                int ans = getAns(Questions.question);
                Button b = (Button)v;
                int buttonText = Integer.parseInt(b.getText().toString());
                if(ans == buttonText) {
                    Questions.points++;
                }
            }
        });
            }
        questionView.setText(Questions.points);

        // end of for loop
    }
private void getQuestionsFromDb(Firebase ref2) {
    // TODO Auto-generated method stub

    ref2.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
        System.out.println("aaa");
            String question = (String) snapshot.child("question").getValue();
            String ans1 =  snapshot.child("ans1").getValue().toString();
            String ans2 =  snapshot.child("ans2").getValue().toString();
            String ans3 =  snapshot.child("ans3").getValue().toString();
            String ans4 =  snapshot.child("ans4").getValue().toString();
            Questions.question = question;
            Questions.ans1 = ans1;
            Questions.ans2 = ans2;
            Questions.ans3 = ans3;
            Questions.ans4 = ans4;
        }
        @Override
        public void onCancelled(FirebaseError arg0) {
            // TODO Auto-generated method stub
        }
    });
}

private int getAns(String ansString) {
    // TODO Auto-generated method stub
    String[] parts = ansString.split("\\b");
    String ans;
    if(parts.length == 5) {
        ans = parts[0] + parts[1] + parts[2] + parts [3] + parts [4];
    }
    else {
        ans = parts[0] + parts[1] + parts[2];
    }
    return Integer.parseInt(ans);
}

As you can see i'm also using outer class that have static variables in order to save the questions and the answers and use them out of the inner class.

I hope someone can help.

André Kool
  • 4,880
  • 12
  • 34
  • 44
  • Welcome to the wonderfull world of asynchronous programming. Because all firebase functions are asynchronous it means the rest of your program isn't waiting for it to finish. Take a look at [this answer](http://stackoverflow.com/questions/33203379/setting-singleton-property-value-in-firebase-listener/33204705#33204705) – André Kool May 17 '16 at 19:07
  • What is the issue? – Gabriele Mariotti May 17 '16 at 20:15

1 Answers1

0

In your case you should add more listeners addListenerForSingleValueEvent() to get all the data you need or rewrite your logic using addValueEventListener. In that case your data will be actual at any time.

Ivan Vovk
  • 929
  • 14
  • 28