简体   繁体   中英

Reading an object from a Firebase database in android studio

Using Android Studio and Firebase, i'm trying to write and read some data.

I have a Pub Class which contains the folowing:

package com.example.jef.pubbuddy.Database;
import java.util.ArrayList;
public class Pub {
private String name;
private float longitude;
private float lattitude;
private ArrayList<Pub> Pubs = new ArrayList<>();
public Pub() {}

public void setName(String name)
{this.name = name;}
public void setLongitude(float longitude)
{this.longitude = longitude;}
public void setLatitude(float lattitude)
{this.lattitude = lattitude;}
public String getName()
{return name;}
public float getLatitude()
{return lattitude;}
public float getLongitude()
{return longitude;}

I write my Pub object to the database using the .push() method. Below is how i write it to the database. It appears just fine in the Firebase console, so I believe the problem doesn't lie here:

Pub p1 = new Pub();
       p1.setName("The name of the pub");
       p1.setLatitude((float) 4.699545);
       p1.setLongitude((float) 50.878267);
myRef.child("PUSH_TEST").push().setValue(p1);

Afterwards I try to read it using the following code. Please note the message method is just used to append some information to a TextView, so i'm able to debug on my physical device. However, none of the listener events get triggered. Does anyone knows what i'm doing wrong here? Already followed the official firebase documentation and the "Firebase in a weekend" training videos. Also looked up countless answers here on Stackoverflow, but I can't seem to make it work.

Thanks in advance.

public class Database extends AppCompatActivity {
private TextView tv;
int messages;
private ArrayList<Pub> pubList = new ArrayList();
private FirebaseDatabase database;
private DatabaseReference myRef;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_database);

    database = FirebaseDatabase.getInstance();
    myRef = database.getReference();
    init();
    writeData();
    message("creating and attaching the listener");
    ChildEventListener myListener = new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s)
        {
            message("The childEvent triggered");
            Pub p = dataSnapshot.getValue(Pub.class);
            message("The name of this pub = " + p.getName());
            pubList.add(p);
        }
        @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(DatabaseError databaseError) {}
    };
    myRef.child("PUSHTEST").addChildEventListener(myListener);
}

Everything is correct, except this:

Here you set the value:

myRef.child("PUSH_TEST").push().setValue(p1);

and here you retrieve the value:

myRef.child("PUSHTEST").addChildEventListener(myListener);

the child that you wrote is wrong as it is not in your database. So just change it into this:

myRef.child("PUSH_TEST").addChildEventListener(myListener);

the name inside child(..) needs to be the same as in your database

You write data to "PUSH_TEST" child and trying to read from "PUSHTEST". Make it same.

For not getting similar errors in future, create a class called "Constants.java" and add constant strings inside it. Like,

 public class Constants {

  public static final String CHILD_NODE="PUSH_TEST";

 }

So that , you can use this constant, where ever u need. Just call Constants.CHILD_NODE . So there will not be such errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM