So basically what I'm trying to do is 'get the names of teams from an API. Then based on the name of team, I have already stored team's country flag's image url in the firebase database. So after I get the Team name from the API, I'm trying to get the url of the following team from the database'. The problem I'm facing is 'same Url (url of first team) is being loaded to all team's url'.
JSON Code of handcoded urls of the flags:
{"Country_images_url" : {
"india a" : {
"imageURL" : "url_1"},
"india b" : {
"imageURL" : "url_2" } } }
In this for() loop I'm retrieving team's name from an API:
DatabaseReference database_country_images_url = FirebaseDatabase.getInstance().getReference("Country_images_url");
ArrayList<CricketScore> details = new ArrayList<CricketScore>;
for(int i=0, i< length; i++){
String team_1_name;
String team_2_name; // both this strings are filled with valued obtained from API
String team_1_url = url_of_team(team_1_name); // user-defined method
String team_2_url = url_of_team(team_2_name);
// And then I'm saving this data in an ArrayList
CricketScore live = new CricketScore(team_1_name, team_2_name, team_1_url, team_2_url);
details.add(live);
}
This is the user-defined method:
public String url_of_team(String team){
DatabaseReference db_url = database_country_images_url.child(team.toLowerCase()).child("imageURL");
db_url.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
url = dataSnapshot.getValue(String.class);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return url;
}
}
Code works fine but the only prblm I'm getting is that same URL is loaded in Arraylist. I don't understand what is going wrong. Please help me solve this.