why cannot scan data from firebase

asdfg :

I get data from firebase in onStart method and put them in an Arraylist(user_words). But in onCreateView method my Arraylist looks empty. However in onstart arraylist is looking full. I did the same thing in different classes and it worked. But this time it fails. I dont understand what is my fault?

private DatabaseReference databaseReference2 = FirebaseDatabase.getInstance().getReference("kullanici_kelime");

    private ArrayList<User_word> user_words= new ArrayList<>();

    @Override
    public void onStart(){
        super.onStart();

        databaseReference2.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                user_words.clear();

                for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {

                    User_word user_word= new User_word();
                    user_word.setMean(postSnapshot.child("mean").getValue(String.class)) ;
                    user_word.setName(postSnapshot.child("name").getValue(String.class));
                    user_word.setKey(postSnapshot.child("key").getValue(String.class));
                    user_word.setKey(postSnapshot.child("id").getValue(String.class));

                    user_words.add(user_word);
                }
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view =inflater.inflate(R.layout.fragment_frag3, container,false);

        rv= (RecyclerView)view.findViewById(R.id.rv2);
        rv.setHasFixedSize(true);
        rv.setLayoutManager(new LinearLayoutManager(this.getActivity()));

        Log.e("wordssize",Integer.toString(user_words.size()));
        adapter= new CardAdapter2_2(getActivity(),user_words);
        rv.setAdapter(adapter);

        return view;
    }

database structure

Doug Stevenson :

addValueEventListener is asynchronous and returns immediately, before the database query is complete. The callback you pass to it will be invoked some time later, whenever the data is available. You shouldn't try to use the array until that data is populated, which means you should only update the UI of your activity until that callback is finally invoked.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=26940&siteId=1