1

I am making an app that reads some data from a firebase realtime database and then uses that data to create a recycle view in android.

My first issue is that my string are not being saved in the ArrayList that is initialize in the OnCreateView method.

My second issue is that firebase realtime database is only being read from when data is changed and only twice, I need it to be read from eveytime the app is opened.

This is in my onCreateView method

 public View onCreateView(@NonNull LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
    HomeViewModel homeViewModel =
            new ViewModelProvider(this).get(HomeViewModel.class);
    
    binding = FragmentHomeBinding.inflate(inflater, container, false);
    root = binding.getRoot();

    blindsowned = new ArrayList<>(); // arraylist for getting users owned blinds
    String userkey = ""; //the user id from firebase, attend from login screen
    // this is how the app get the users owned blinds from the database
    getBlindsOwned(userkey);// this function will have the code to connect to database

    System.out.println("line 51 "+blindsowned.toString());

    // these are test cased for the blinds that will be appear in the homepage
    testcase.add(new HomeBlinds("test 1"));
    testcase.add(new HomeBlinds("test 2"));


    // this is code that is used to populate the recyclerview for the blinds
    recyclerView = (RecyclerView) root.findViewById(R.id.home_recycler_view);
    homeRecyclerViewAdapter = new HomeRecyclerViewAdapter(testcase,getContext());
    recyclerView.setAdapter(homeRecyclerViewAdapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    applySettings();
    return root;
}

This is my method where that data is being read from: the variable userkey will contain the user firebase id

public void getBlindsOwned(String userkey){
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Admin").child("Owned");
    ArrayList<String> ob = new ArrayList<String>();
    ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            for(DataSnapshot dataSnapshot : snapshot.getChildren()){
                String test = dataSnapshot.getValue().toString();
                System.out.println("the blinds owned " + test);
                blindsowned.add(test);
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    });
}

This is my firebase realtime database structure firebase

Any help is appreciated.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Amit Punit
  • 21
  • 2
  • The simplest solution I can think of would be to use the [Firebase-UI library](https://stackoverflow.com/questions/49383687/how-can-i-retrieve-data-from-firebase-to-my-adapter/49384849). If you, however, consider at some point in time try using [Cloud Firestore](https://firebase.google.com/docs/firestore/), here you can find a useful [resource](https://medium.com/firebase-tips-tricks/how-to-filter-firestore-in-real-time-using-jetpack-compose-952abaab15c5) that can help you achieve that. – Alex Mamo Jul 18 '22 at 06:53

0 Answers0