0
public void schedulematchfunction()
    {

        Query query = databasePosts.child("team").orderByChild("teamName");


        query.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                final List<String> titleList = new ArrayList<String>();
                for(DataSnapshot dataSnapshot1: dataSnapshot.getChildren()) {
                    team1Name = team1.getSelectedItem().toString();
                    team2Name = team2.getSelectedItem().toString();

                    String teamId = (String) dataSnapshot1.child("teamId").getValue();
                    String teamName = (String) dataSnapshot1.child("teamName").getValue();


                    Player player1=  dataSnapshot1.child("player1").getValue(Player.class);
                    Player player2=  dataSnapshot1.child("player2").getValue(Player.class);
                    Player player3= dataSnapshot1.child("player3").getValue(Player.class);
                    Player player4=  dataSnapshot1.child("player4").getValue(Player.class);
                    Player player5=  dataSnapshot1.child("player5").getValue(Player.class);
                    Player player6= dataSnapshot1.child("player6").getValue(Player.class);
                    Player player7=  dataSnapshot1.child("player7").getValue(Player.class);
                    Player player8=  dataSnapshot1.child("player8").getValue(Player.class);
                    Player player9=  dataSnapshot1.child("player9").getValue(Player.class);
                    Player player10=  dataSnapshot1.child("player10").getValue(Player.class);
                    Player player11=  dataSnapshot1.child("player11").getValue(Player.class);
                    Player player12= dataSnapshot1.child("player12").getValue(Player.class);




                    if(teamName==team1Name)
                    {
                        finalTeam1Schedule.setTeamId(teamId);
                       finalTeam1Schedule.setTeamName(teamName);
                       finalTeam1Schedule.setPlayer1(player1);
                        finalTeam1Schedule.setPlayer2(player2);
                        finalTeam1Schedule.setPlayer3(player3);
                        finalTeam1Schedule.setPlayer4(player4);
                        finalTeam1Schedule.setPlayer5(player5);
                        finalTeam1Schedule.setPlayer6(player6);
                        finalTeam1Schedule.setPlayer7(player7);
                        finalTeam1Schedule.setPlayer8(player8);
                        finalTeam1Schedule.setPlayer9(player9);
                        finalTeam1Schedule.setPlayer10(player10);
                        finalTeam1Schedule.setPlayer11(player11);
                        finalTeam1Schedule.setPlayer12(player12);
                    }
                    if(teamName==team2Name)
                    {

                        finalTeam2Schedule.setTeamId(teamId);
                        finalTeam2Schedule.setTeamName(teamName);
                        finalTeam2Schedule.setPlayer1(player1);
                        finalTeam2Schedule.setPlayer2(player2);
                        finalTeam2Schedule.setPlayer3(player3);
                        finalTeam2Schedule.setPlayer4(player4);
                        finalTeam2Schedule.setPlayer5(player5);
                        finalTeam2Schedule.setPlayer6(player6);
                        finalTeam2Schedule.setPlayer7(player7);
                        finalTeam2Schedule.setPlayer8(player8);
                        finalTeam2Schedule.setPlayer9(player9);
                        finalTeam2Schedule.setPlayer10(player10);
                        finalTeam2Schedule.setPlayer11(player11);
                        finalTeam2Schedule.setPlayer12(player12);
                    }
                }

            }
            @Override
            public void onCancelled(DatabaseError databaseError)
            {
                Toast.makeText(ScheduleMatchActivity.this,databaseError.getMessage(),Toast.LENGTH_LONG).show();
            }
        });

        ScheduleOverNumber=""+ScheduleOver.getValue();
        String ScheduleMatchId = databaseSchedulematch.push().getKey();
        ScheduleMatch scheduleMatch = new ScheduleMatch(ScheduleMatchId,finalTeam1Schedule,finalTeam2Schedule,
                dateschedule.getDayOfMonth()+"/"+ (dateschedule.getMonth() + 1)+"/"+dateschedule.getYear(),
                timeschedule.getCurrentHour()+"/"+ timeschedule.getCurrentMinute()+" -(Pak Time)",ScheduleOverNumber);
        databaseSchedulematch.child(ScheduleMatchId).setValue(scheduleMatch);
    }

I'm trying to copy data from team node to schedulematch node I've made class of team and then saved data now i am trying to copy data from team to schedulematch by creating another team object and setting data using setters but it is not working please tell what I'm doing wrong. and by using new object of team i'm storing data in firebase but it is giving everything wellin firebase except team data. Is there anyother method?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • 2
    "It is not working" is not very much information to work with Please edit the question to explain what this code is actually doing that's different then what you expect, along with any other debugging information. Logging is always helpful. – Doug Stevenson Aug 14 '20 at 19:24
  • you are fetching same player for all 12 players there, are you sure there's a match between team name and you are not passing null finalTeam1Schedule and finalTeam2Schedule variables. – Haider Saleem Aug 14 '20 at 19:34
  • @HaiderSaleem would you please explain what you are trying to say? – Rashid Noor Aug 14 '20 at 19:50
  • This is not how you compare strings in Java: `if(teamName==team1Name)`. Instead you'll want to do `if (teamName.equals(team1Name))...` See https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Frank van Puffelen Aug 14 '20 at 20:11
  • @FrankvanPuffelen actually still not woking, basically in debugging I've checked program is not entering into onDataChage function and bypassing it. – Rashid Noor Aug 14 '20 at 22:51
  • The string comparison I pointed out is certainly a problem in what you shared, so if it isn't the problem you're posting about: simplify your code to the minimum that is needed to reproduce the problem. See [how to create a minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) – Frank van Puffelen Aug 14 '20 at 23:29
  • More likely than `onDataChange` not being called, is that your `databaseSchedulematch.child(ScheduleMatchId).setValue(scheduleMatch)` runs before `onDataChange` is called. That is because data is loaded from Firebase asynchronously. Any code that needs the data needs to be *inside* `onDataChange` *or* be called from there. See https://stackoverflow.com/questions/50434836/getcontactsfromfirebase-method-return-an-empty-list/50435519#50435519 for more on that – Frank van Puffelen Aug 14 '20 at 23:31

0 Answers0