0

Answer needed urgently. I'm trying to retrieve a single value school name as marked in the snapshot attached below and populate it to my android spinner from my firebase-database

private class FireBaseConnection{
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference rootRef = database.getReference().child("root");

    DatabaseReference reg_schools = rootRef.child("reg_schools");
}



private ArrayList<String> retrieveAllSchools(){
    pBar.setMessage("Please Wait..Retrieving Schools...");
    pBar.show();

    FireBaseConnection fireBaseConnection = new FireBaseConnection();
    String key = fireBaseConnection.reg_schools.push().getKey();

    fireBaseConnection.reg_schools.child(key).child("school_name").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            for(DataSnapshot snapshot : dataSnapshot.getChildren()){

                    resultProcessorObject = snapshot.getValue(SchoolObject.class);
                   schools_retrieved =  resultProcessorObject.getSchool_name();
                   schools.add( schools_retrieved);
                //schools = (ArrayList<Object>) snapshot.getValue();
                 // Toast.makeText(Main2Activity.this, "Schools are : " + schools, Toast.LENGTH_SHORT).show();

            }


            Toast.makeText(Main2Activity.this, "Schools are : " + schools, Toast.LENGTH_SHORT).show();
            pBar.dismiss();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    return schools;
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Uncle Sam
  • 61
  • 2
  • 10
  • https://medium.com/google-developers/why-are-firebase-apis-asynchronous-callbacks-promises-tasks-e037a6654a93 – John O'Reilly Mar 19 '18 at 10:11
  • and what is the problem? please specify your question... You fetched your data, now pass it to the UI and populate your spinner – Idanatz Mar 19 '18 at 10:41
  • Possible duplicate of [How to return dataSnapshot value as a result of a method?](https://stackoverflow.com/questions/47847694/how-to-return-datasnapshot-value-as-a-result-of-a-method) – Alex Mamo Mar 19 '18 at 11:26
  • Please see the duplicate to see the reason. You can also take a look at this **[video](https://www.youtube.com/watch?v=OvDZVV5CbQg)** for a better understanding. – Alex Mamo Mar 19 '18 at 11:27
  • @Idanatz..I can't retreive any result yet..The toast message i attached for testing my result just retreives an empty array – Uncle Sam Mar 19 '18 at 12:38
  • wow..Thanks Alex Mamo..your video played the magic – Uncle Sam Mar 19 '18 at 13:36
  • **TYPO** alert: it's spelled **retrieve** - not "retreive" as you constantly used – marc_s Apr 14 '18 at 20:10
  • oh ok..Thank you @marc_s – Uncle Sam Apr 15 '18 at 21:43

1 Answers1

0

So I want to answer my question..I made it work with the help of @Alex Mamo video

so everything remained unchanged..all i did was tweak my code a bit

Instead of

fireBaseConnection.reg_schools.child(key).child("school_name").addValueEventListener(){
    //Then my codes here
}

Instead i did

fireBaseConnection.reg_schools.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

        for(DataSnapshot snapshot : dataSnapshot.getChildren()){
            resultProcessorObject = snapshot.child("school_name").getValue(String.class);
            //schools_retreived =  resultProcessorObject.getSchool_name();
            schools.add(resultProcessorObject);
            count = dataSnapshot.getChildrenCount();
            Toast.makeText(Main2Activity.this, "Total number of " + count + " schools were retreived ", Toast.LENGTH_SHORT).show();
            // schools = (ArrayList<Object>) snapshot.getValue();
            // Toast.makeText(Main2Activity.this, "Schools are : " + schools, Toast.LENGTH_SHORT).show();
        }

        Toast.makeText(Main2Activity.this, "Schools are : " + schools, Toast.LENGTH_SHORT).show();
        pBar.dismiss();
    }


    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

So Basically i used the snapshot to reference the node "school_name" i'm looking for in the onDataChange Method

Rhusfer
  • 571
  • 1
  • 9
  • 14
Uncle Sam
  • 61
  • 2
  • 10