0

I have a class. A constructor of the class takes a couple of parameters, but I need to have acces to 2 different Firebase references to create an object of my class. For now I'm taking data from my Firebase Realtime Database using callbacks to access it in different classes (the 1st answer here: How to return a DocumentSnapShot as a result of a method?).

A piece of code as an example how I get data from 1 reference:

public class Database {
    ArrayList<TournamentParameters> tournamentsList = new ArrayList<TournamentParameters>();
    ArrayList<String> tournamentsNames = new ArrayList<String>();
    DatabaseReference tournamentsReference = database.getReference("Tournaments");
    public void readTournaments(final TournamentsCallback tournamentsCallback){
        tournamentsReference.addValueEventListener(new ValueEventListener() {
            //@RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                //filling tournamentsList, tournamentsNames
                ...
                tournamentsCallback.onCallback(tournamentsList, tournamentsNames);
            }
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }
    public interface TournamentsCallback {
        void onCallback(ArrayList<TournamentParameters> tournamentsList, ArrayList<String> tournamentsNames);
    }
}
public class RatingFragment{
    Database base = new Database();
        base.readTournaments(new Database.TournamentsCallback() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onCallback(ArrayList<TournamentParameters> tournamentsList, ArrayList<String> tournamentsNames) {
                ArrayList<PlayerParameters> playersList = new ArrayList<PlayerParameters>();
                HashMap<String, Integer> rating = new HashMap<String, Integer>();
                ListView listView = (ListView) view.findViewById(R.id.ratingListView);
            //some lines of code
            ...
}

So I write my code inside onCallback method which provides me an access to data from one of my references.

But how to perform working with two references at the same time?

b1sk1
  • 23
  • 3
  • The code looks fine to me. What is the problem? Specifically: what line of code that you shared doesn't do what you want/expect it to do? – Frank van Puffelen May 26 '20 at 20:12
  • @FrankvanPuffelen it's all right with the code, but as I said, it gets data only from 1 reference. The question is how to get data from 2 references at the same time – b1sk1 May 26 '20 at 21:13

0 Answers0