0

Try to return data onDatachange and I know why it returns null. I solved it with using adapter.

But I also have a method called "FindWord" in the same class. How can I get data from onDataChange in this method?

public ArrayList<Word2> AllWords(final CardAdapter4_4 adapter){

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

                words.clear();

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

                    Word2 word= new Word2();
                    word.setMean(postSnapshot.child("mean").getValue(String.class)) ;
                    word.setName(postSnapshot.child("name").getValue(String.class));
                    word.setControlled(postSnapshot.child("controlled").getValue(Boolean.class));
                    word.setAdded_by(postSnapshot.child("added_by").getValue(String.class));
                    word.setKey(postSnapshot.child("key").getValue(String.class));
                    words.add(word);
                }
                count=words.size();
                adapter.notifyDataSetChanged();
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
        return words;
    }
public Word2 FindWord( String kelime){
        Word2 w=null;
        ArrayList<Word2> array = AllWords();
        for(int i=0;i<array.size();i++){
            if(kelime.equals(array.get(i).getName())){
                w=array.get(i);
            }
        }
        if(w!=null){
            return w;}
        else return null;
    }
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
asdfg
  • 27
  • 6
  • You cannot do that. What you can do instead is move the code in `FindWord` into the `onDataChange` function. – Kidus Mar 04 '20 at 21:29
  • Please check the duplicate to see why do you have this behavior and how can you solve this using a custom callback. – Alex Mamo Mar 05 '20 at 10:28

0 Answers0