YaraR
YaraR

Reputation: 111

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?

Upvotes: 0

Views: 2023

Answers (2)

nakul parashar
nakul parashar

Reputation: 85

Modify your code

  1. Initialize variables

    private FirebaseDatabase mFirebaseDatabase;
    private DatabaseReference mMessagesDatabaseReference;
    private ChildEventListener mChildEventListener;
    //for list view
    private ListView mListView;
    private MessageAdapter mAdapter;
    
  2. In onCreate

    mFirebaseDatabase=FirebaseDatabase.getInstance();
    mMessagesDatabaseReference=mFirebaseDatabase.getReference().child("search").child("country").child("city").child("blood group"); 
    List<String> data = new ArrayList<>();
    mAdapter = new Adapter(this, R.layout.data,data);
    mListView.setAdapter(mAdapter);
    
    dataRead();
    
  3. dataRead method

    private void dataRead() {
        if ( mChildEventListener == null) {
        mChildEventListener = new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            String value = dataSnapshot.getValue(String.class);
            mAdapter.add(value);
            }
    
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {}
            public void onChildRemoved(DataSnapshot dataSnapshot) {}
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {}
            public void onCancelled(DatabaseError databaseError) {}
        };
        mMessagesDatabaseReference.addChildEventListener( mChildEventListener);
    }
       }
    
  4. String.class should look like this. Note the public variables and empty constructor.

    public class User {
    public String user;
    public String emailId;
    public long timeStampStart;
    public long timeStampStop;
    public long diff;
    
    public User()
    {
    
    }
    public User(String user,String emailId,long timeStampStart,long timeStampStop,long diff)
    {
    this.user=user;
    this.emailId=emailId;
    this.timeStampStart=timeStampStart;
    this.timeStampStop=timeStampStop;
    this.diff=diff;
    }
    
    
    public String GetUser()
    {
    return user;
    }
    
    public String GetEmailId()
    {
    return emailId;
    }
    
    public long GetTimeStampStart()
    {  
    return timeStampStart;
    }
    public long GetTimeStampStop()
    {
    return timeStampStop;
    }
    
    public long GetDiff()
    {
    return diff;
    }
    
    public void setUser(String user)
    {
    this.user=user;
    }
    public void setEmailId(String emailId)
    {
    this.emailId=emailId;
    }
    public void setTimeStampStart(long timeStampStart)
    {
    this.timeStampStart=timeStampStart;
    }
    public void setTimeStampStop(long timeStampStop)
    {
    this.timeStampStop=timeStampStop;
    }
    
    public void setDiff(long diff)
    {
    this.diff=diff;
    }
    }
    
  5. Now database should look like this. Modify according to your need

     search
        country
      -city
        -blood group
          -123
            abc:something
            bcd:something
    

    and let me know if any problem arises

Upvotes: 0

ColdSpike
ColdSpike

Reputation: 196

Check if the rules in your database are set to true if you are not using authentication

{
  "rules": {
    ".read": "true",
    ".write": "true"
  }
}

if that is not the problem then check what your datasnapshot is returning.If your database is something like this

root
  -country
     -city
       -bloodgroup
          -B+
            -123:something
            -111:something

then you have to give the key for which you want to get the value. example

datasnapshot.child("B+").child("123").getValue();//for first child i.e 123 datasnapshot.child("B+").child("111").getValue();//for second child i.e 111

else 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.

As 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.

class mClass{
 private String first;
 private String second;
 mClass(){}
 mClass(String first,String second){
    this.first=first;
    this.second=second;
 }
 public String getFirst(){
  return this.first;
 }
 public String getSecond(){
  return this.second;
 }
}

Now you can get the data in this class.

mClass mclass = datasnapshot.getValue(mClass.class);

Now where you want to use this data just call the getter function.

text.setText(mclass.getFirst());

Upvotes: 1

Related Questions