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?