0

I have a arraylist which name is exampleList, ı got the value from my firebase database and add them to exampleList. I have checked that there are some values in the exampleList arraylist but out of scope values are gone away. I couldn't understand the reason. Could you please help me.

Note : If I open the comment between HERE lines in the code below ı can see "QUESTION1", "ANSWER1", "A", "B", "C", "D", "E" values on the screen.

Here is my code below;

dbGetData.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            //soru sayısı kadar döner.
            for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
                //PLEASECHECKME NODE U ALTINDAKİ SORULARI ÇEKMİŞ OLDUK
                Question = dataSnapshot1.child("question").getValue().toString();
                Answer = dataSnapshot1.child("answer").getValue().toString();
                A = dataSnapshot1.child("answerA").getValue().toString();
                B = dataSnapshot1.child("answerB").getValue().toString();
                C = dataSnapshot1.child("answerC").getValue().toString();
                D = dataSnapshot1.child("answerD").getValue().toString();
                E = dataSnapshot1.child("answerE").getValue().toString();
                exampleList.add(new ExampleItem(Question, Answer, A, B, C, D, E));
            }
        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
        }
    });
    //**************************************************************************************************
    mRecyclerView = findViewById(R.id.recyclerView);
    mAdapter = new ExampleAdapter(exampleList);
    mRecyclerView.setAdapter(mAdapter);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

    *****HERE*****

    //exampleList.add(new ExampleItem("QUESTION1", "ANSWER1", "A", "B", "C", "D", "E"));

    *****HERE*****

    mAdapter.notifyDataSetChanged();
bilbey
  • 1
  • Data is loaded from Firebase asynchronously. By the time your `*****HERE*****` code executes, the `onDataChange` hasn't been called yet. If you put some breakpoints on these lines and run in a debugger, or put some logging in there, you'll see this. For this reason, any code that needs the data, needs to be *inside* the `onDataChange`, which includes in your case the `mAdapter.notifyDataSetChanged();` call. Also see https://stackoverflow.com/questions/50434836/getcontactsfromfirebase-method-return-an-empty-list/50435519#50435519 – Frank van Puffelen Sep 08 '20 at 16:06
  • Thanks for your support Frank :) Your advice solved my problem. – bilbey Sep 09 '20 at 09:30

0 Answers0