0

Im trying to get list of strings from a child and store it in a list of string , but idk what is wrong it is not getting any data from firebase at all

Here is the Data Fomat in Firebase Real Time Data Base

{

"art": [

{

"MrIncredibleArt": "stringexample4"

},

{

"GhostFace": "stringexample5"

},

...

],

"nsfw": [

{

"AnyLoser1": "stringtrollface1"

},

{

"AnyLoser2": "stringexample2"

},

...

]

}

Here is the method which get the data from firbase and store it in a list of string

 private List<String> initAsciiArts_Meme() {
        DatabaseReference databaseRef = FirebaseDatabase.getInstance().getReference().child("art");
        List<String> dataList =  new ArrayList<>();
        databaseRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NotNull DataSnapshot dataSnapshot) {


                for (DataSnapshot memeSnapshot : dataSnapshot.getChildren()) {
                    for (DataSnapshot kvpSnapshot : memeSnapshot.getChildren()) {
                        String key = kvpSnapshot.getKey();
                        String value = kvpSnapshot.getValue(String.class);
                        Log.i("Firebase_Info", key + ": '" + value + "'");
                        dataList.add(value);
                    }
                }


            }

            @Override
            public void onCancelled(@NotNull DatabaseError databaseError) {
                // Handle any errors that may occur
                Log.e("Firebase", "Failed to retrieve art data: " + databaseError.getMessage());
                Toast.makeText(requireContext(), "Failed to retrieve art data: " + databaseError.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
        return dataList;
    }

Here is the OnCreate

public class MemeFragment extends Fragment {

   List<String> asciiArts = new ArrayList<>();

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.meme_fragment, container, false);
        asciiArts = initAsciiArts_Meme();
try {
            adapter= new MemeAdapter_Meme(asciiArts, requireContext(), this);
            recyclerView.setHasFixedSize(true);
            recyclerView.setAdapter(adapter);
        } catch (Exception e) {
            e.printStackTrace();
            // Handle the exception here
        }

        return view;
    }
Vasant Raval
  • 257
  • 1
  • 12
  • 31
  • Your `return dataList;` returns the data list before any of the `onDataChange` has been called, and thus before any of the `dataList.add(value);` has executed. If you set breakpoints on those lines and run the code in a debugger, you can see this quickly. Any code that needs the data from the database must be inside the `onDataChange`, be called from there, or be otherwise synchronized. Such as by calling `notifyDataSetChanged` as I said in my answer to your previous questions. – Frank van Puffelen Jul 01 '23 at 20:38
  • If you understand Kotlin, I think that this [resource](https://medium.com/firebase-tips-tricks/how-to-read-data-from-firebase-realtime-database-using-get-269ef3e179c5) will help. Here is the corresponding [repo](https://github.com/alexmamo/RealtimeDatabase). – Alex Mamo Jul 03 '23 at 08:31

0 Answers0