-1

I have an online quiz app in Firebase. Shuffle method does not work. Every time question come the same interval.

Each time the questions come the same. Comes with the same sequence. I do not understand why that happens.

The question is actually very simple, but I have to extend it for acceptance.

private void loadQuestions(String categoryId) {

        //First, clear list if have old questions
        if (Common.questionList.size()>0)
            Common.questionList.clear();

        question.orderByChild("categoryId").equalTo(categoryId).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                    Question question = postSnapshot.getValue(Question.class);
                    Common.questionList.add(question);
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {}
        });

        //Random list
        Collections.shuffle(Common.questionList);
    }


public class Question {
    private String sul, bri, ki, cu, dor, du, categoryId, sek;

    public Question() {
    }

    public Question(String sul, String bri, String ki, String cu, String dor, String du, String categoryId, String sek) {
        this.sul = sul;
        this.bri = bri;
        this.ki = ki;
        this.cu = cu;
        this.dor = dor;
        this.du = du;
        this.categoryId = categoryId;
        this.sek = sek;
    }

    public String getSul() {
        return sul;
    }

    public void setSul(String sul) {
        this.sul = sul;
    }

    public String getBri() {
        return bri;
    }

    public void setBri(String bri) {
        this.bri = bri;
    }

    public String getKi() {
        return ki;
    }

    public void setKi(String ki) {
        this.ki = ki;
    }

    public String getCu() {
        return cu;
    }

    public void setCu(String cu) {
        this.cu = cu;
    }

    public String getDor() {
        return dor;
    }

    public void setDor(String dor) {
        this.dor = dor;
    }

    public String getDu() {
        return du;
    }

    public void setDu(String du) {
        this.du = du;
    }

    public String getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(String categoryId) {
        this.categoryId = categoryId;
    }

    public String getSek() {
        return sek;
    }

    public void setSek(String sek) {
        this.sek = sek;
    }
}


public class Common {
    public static String categoryId,categoryName;
    public static User currentUser;
    public static List<Question> questionList = new ArrayList<>();
    public static final String STR_PUSH = "pushNotification";

}
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • 1
    If Stack Overflow tells you that you have too much code and too little text, don't just add more dummy text. Instead read: [how to create a minimal, complete verifiable example](http://stackoverflow.com/help/mcve). – Frank van Puffelen Aug 04 '18 at 21:36

1 Answers1

0

When you using the following line of code:

Collections.shuffle(Common.questionList);

Outside the onDataChange(), it means that you are trying to shuffle an empty list. This is happening because this method has an asynchronous behavior which means that by the time you are trying to shuffle that list outside that method, the data hasn't finished loading yet from the database and that's why is not accessible.

A quick solve for this problem would be to move that line of code inside the onDataChange() method, otherwise I recommend you see the last part of my anwser from this post in which I have explained how you can use a String (it can be any other object, even a List) outside the onDataChange() method using a custom callback. You can also take a look at this video for a better understanding.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193