0

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.

Vivek Thakkar
  • 131
  • 3
  • 11

1 Answers1

0
 CricketScore live = new CricketScore();

Use setter method to set values here team_1_name, team_2_name.

 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);
            //Set the data in object here - team_1_url, team_2_url
            details.add(live);  

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

        return url;
    }
    }

Try to add data in arraylist as shown above inside the onDataChange()

Raj
  • 2,997
  • 2
  • 12
  • 30
  • But I want to add both team's url in the same Arraylist. So how should I do that? – Vivek Thakkar Jul 03 '18 at 19:23
  • Try this, all names will be added in same array list. – Raj Jul 03 '18 at 19:27
  • See, I have just edited the for() the way I want to add data in ArrayList. Now how should I add the url inside the onDataChange() ? – Vivek Thakkar Jul 03 '18 at 19:38
  • I have edited my answer @VivekThakkar – Raj Jul 03 '18 at 19:53
  • I tried the way you said, but ArrayList doesn't get filled completely. Like most of the ArrayList's index doesn't contain the urls. There's one thing I noticed is that even though I'm setting the URL variable's value as null in every for() loop iteration, it saves the same value of URL in it. How's that even possible? I really don't know whats going wrong – Vivek Thakkar Jul 04 '18 at 05:44