复制:在课程

时间:2018-06-25 15:03:11

标签: java android firebase firebase-realtime-database

当我尝试使用Firebase获取数据时,出现此问题:

  

在com.school.models.EmBeacon类上找不到0C:F3:EE:12:E0:4C的设置器/字段

     

在com.school.models.EmBeacon类上找不到0C:F3:EE:12:E0:5C的设置器/字段

     

在com.school.models.EmBeacon类上找不到0C:F3:EE:12:E0:E5的设置器/字段

这是我的数据结构:

enter image description here

这是获取我所有数据的代码:

mDatabase.child("beacons").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                EmBeacon mBeacon = dataSnapshot.getValue(EmBeacon.class);
                Log.i(TAG, "onDataChange: "+mBeacon);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Log.w(TAG, "onCancelled:", databaseError.toException());
            }
        });

和班级:

public class EmBeacon {

    private String proximityUuid;
    private int major;
    private int minor;
    private String addressMac;

    public EmBeacon() {
    }

    public EmBeacon( String addressMac) {
        this.addressMac = addressMac;
    }

    public EmBeacon(String proximityUuid, int major, int minor, String addressMac) {
        this.proximityUuid = proximityUuid;
        this.major = major;
        this.minor = minor;
        this.addressMac = addressMac;
    }

    public String getProximityUuid() {
        return proximityUuid;
    }

    public void setProximityUuid(String proximityUuid) {
        this.proximityUuid = proximityUuid;
    }

    public int getMajor() {
        return major;
    }

    public void setMajor(int major) {
        this.major = major;
    }

    public int getMinor() {
        return minor;
    }

    public void setMinor(int minor) {
        this.minor = minor;
    }

    public String getAddressMac() {
        return addressMac;
    }

    public void setAddressMac(String addressMac) {
        this.addressMac = addressMac;
    }

    @Override
    public String toString() {
        return "EmBeacon{" +
                "proximityUuid='" + proximityUuid + '\'' +
                ", major='" + major + '\'' +
                ", minor='" + minor + '\'' +
                ", addressMac='" + addressMac + '\'' +
                '}';

你知道这里有什么问题吗?

1 个答案:

答案 0 :(得分:4)

/beacons下有多个信标,因此您需要在onDataChange中循环这些信标:

mDatabase.child("beacons").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        for (DataSnapshot beaconSnapshot: dataSnapshot.getChildren()) {
            EmBeacon mBeacon = beaconSnapshot.getValue(EmBeacon.class);
            Log.i(TAG, "onDataChange: "+mBeacon);
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.w(TAG, "onCancelled:", databaseError.toException());
    }
});