Android Studio - Retrieving data from firebase is not working
NickName:YaraR Ask DateTime:2017-03-25T18:51:20

Android Studio - Retrieving data from firebase is not working

I'm working on retrieving some data from Firebase and i followed some Youtube tutorials that were working fine,

but out of nowhere the retrieving is not working anymore,

the path that i'm setting is correct but i get an empty list even though there is data from where i am retrieving.

private ListView mListView;
private ArrayList<String> mHospitalNames= new ArrayList<>();
private Firebase mRefRoot;

this is the path:

mRefRoot=new Firebase("https://prototype-d2a84.firebaseio.com/Search/"+countryName+"/"+mCity+"/"+bloodType);

this is the code that i am using:

   mListView = (ListView) findViewById(R.id.listview);

    final ArrayAdapter<String> mArrayAdapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mHospitalNames);

    mListView.setAdapter(mArrayAdapter);

    mRefRoot.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

            String value = dataSnapshot.getValue(String.class);
            mHospitalNames.add(value);
            mArrayAdapter.notifyDataSetChanged();
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });

this is how my database looks like

enter image description here

can someone tell me whats wrong?

Copyright Notice:Content Author:「YaraR」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/43015468/android-studio-retrieving-data-from-firebase-is-not-working

Answers
ColdSpike 2017-03-25T11:23:52

Check if the rules in your database are set to true if you are not using authentication\n\n{\n \"rules\": {\n \".read\": \"true\",\n \".write\": \"true\"\n }\n}\n\n\nif that is not the problem then check what your datasnapshot is returning.If your database is something like this\n\nroot\n -country\n -city\n -bloodgroup\n -B+\n -123:something\n -111:something\n\n\nthen you have to give the key for which you want to get the value. example\n\ndatasnapshot.child(\"B+\").child(\"123\").getValue();//for first child i.e 123\ndatasnapshot.child(\"B+\").child(\"111\").getValue();//for second child i.e 111\n\nelse if you want the entire set of values then, you can use the above line multiple times with different key or use a class to get all the values at once.\n\nAs per your database you have keys as numbers,i would suggest to change that and keep strings as this would cause problem in retrieving data if you use a class.\n\nclass mClass{\n private String first;\n private String second;\n mClass(){}\n mClass(String first,String second){\n this.first=first;\n this.second=second;\n }\n public String getFirst(){\n return this.first;\n }\n public String getSecond(){\n return this.second;\n }\n}\n\n\nNow you can get the data in this class.\n\nmClass mclass = datasnapshot.getValue(mClass.class);\n\n\nNow where you want to use this data just call the getter function.\n\ntext.setText(mclass.getFirst());\n",


More about “Android Studio - Retrieving data from firebase is not working” related questions

Android Studio - Retrieving data from firebase is not working

I'm working on retrieving some data from Firebase and i followed some Youtube tutorials that were working fine, but out of nowhere the retrieving is not working anymore, the path that i'm setting...

Show Detail

Firebase on android not retrieving data

I have been able to successfully upload and delete data from my firebase repo using the guide here: https://www.firebase.com/docs/android/guide/saving-data.html However if I try and retrieve the d...

Show Detail

Retrieving data from firebase database in android

I'm recently migrated to Firebase. I have gone through Firebase Android api docs for retrieving data from database through JSON. there is method onDataChange(DataSnapshot snapshot) to retrieve data

Show Detail

Retrieving data from Firebase Android Studio

Structure of my Firebase Datebase looks like that: I want to retrieve data from it in such a manner that when I change the value of the "Name" in database it will instantly change in Android Studio.

Show Detail

Android Studio is not reading the retreived data from Firebase realtime database

I am trying to read data from Firebase but it is not read by android studio although I am using the tutorial I tried to copy the link that is sent by Android Studio to Firebase: DatabaseReference...

Show Detail

Data is retrieved from Firebase only once

I am working on Android Studio and I have an activity that retrieves data (latitude and longitude) with a button hit from firebase after typing an ID in a textBox, it retrieves related data to that...

Show Detail

Using Android Loader while retrieving data from Firebase

I am retrieving data from Firebase and use this data to present some information. Since retrieving data takes time I put a CountDownTimer to make the app wait but it is not a nice solution and it d...

Show Detail

Retrieving data from firebase in android in sorting order

first of all the code is work fine for me . data is retrieving from firebase finely i want see the last inserted data in top . but it's working reverse mode. here is code for retrieving . import

Show Detail

Retrieving data from Firebase database to webView in Android

I am building an application with webView and Firebase as database, I have implemented both correctly but the problem is that my webView is not retrieving any data from Firebase database and is sho...

Show Detail

Retrieving data from firebase dynamically in android

I have my database structured like this in firebase: app root: user: uid1: some data uid2: some data helper: ...

Show Detail