POJO中的哈希映射以满足嵌套的Firebase结构

时间:2017-07-20 01:09:26

标签: android firebase data-structures firebase-realtime-database pojo

使用Firebase编写我的第一个应用程序并尝试将嵌套结构放入一个POJO,因为我将在同一屏幕上显示所有内容,但未成功检索内部结构。 这是快照:

{ 
key = -KIqtlY8VQFCuYmi-fWc, 
value = {
    tripName=Paris in June, 
    cityId=paris, 
    arrivalDate=2310943,
    duration=4, 
    days={
        day1={
            -KpMhSTQABFhbBHri0VU={
                keyItem=-KGYdK_RIc3Whm5Vp5WM, 
                orderItem=99, 
                typeItem=att
            },
        day2={
            -KpMhSTQABFhbBHri0AB={
                keyItem=-KGYdK_RIc3Whm5Vp5WM, 
                orderItem=99, 
                typeItem=att
            }
        }
    } 
    } 
}

以下是我的课程:

@IgnoreExtraProperties
public class Trip {
    @Exclude
    private String id;
    private String cityId;
    private String tripName;
    private long arrivalDate;
    private int duration;
    private Map<String, DayTrip> days; // = new HashMap<String, DayTrip>();

    public Trip() {
    }

    public Trip(String cityId, String tripName, long arrivalDate, int duration, Map<String, DayTrip> days) {
        this.cityId = cityId;
        this.tripName = tripName;
        this.arrivalDate = arrivalDate;
        this.duration = duration;
        this.days = days;
    }


    public String getId() {
        return id;
    }

    public String getCityId() {
        return cityId;
    }

    public String getTripName() {
        return tripName;
    }


    public long getArrivalDate() {
        return arrivalDate;
    }


    public int getDuration() {
        return duration;
    }

    public Map<String, DayTrip> getDays() {
        return days;
    }
}

public class DayTrip {
    private Map<String, ScheduledItem> scheduledItems; // = new HashMap<String, ScheduledItem>();

    public DayTrip() {
    }

    public DayTrip(String notes, Map<String, ScheduledItem> scheduledItems) {
        this.scheduledItems = scheduledItems;
    }

    public Map<String, ScheduledItem> getScheduledItems() {
        return scheduledItems;
    }
}

public class ScheduledItem {

    private String keyItem;
    private String typeItem;
    private int orderItem;

    public ScheduledItem() {

    }

    public ScheduledItem(String keyItem, String typeItem, int orderItem) {
        this.keyItem = keyItem;
        this.typeItem = typeItem;
        this.orderItem = orderItem;
    }

    public void setKeyItem(String keyItem) {
        this.keyItem = keyItem;
    }

    public void setTypeItem(String typeItem) {
        this.typeItem = typeItem;
    }

    public void setOrderItem(int orderItem) {
        this.orderItem = orderItem;
    }

    public String getKeyItem() {
        return keyItem;
    }
    public String getTypeItem() {
        return typeItem;
    }
    public int getOrderItem() {
        return orderItem;
    }
    @Exclude
    public Map<String, Object> toMap() {
        HashMap<String, Object> result = new HashMap<>();
        result.put("keyItem", keyItem);
        result.put("typeItem", typeItem);
        result.put("orderItem", orderItem);

        return result;
    }
}

这是我用来检查对象的代码:

    mUserRef1 = mFirebaseRef.child("trips").child(getUid());

    mUserRef1.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot snap: dataSnapshot.getChildren()) {
                Trip trip = snap.getValue(Trip.class);
                for (Map.Entry<String, DayTrip> day: trip.getDays().entrySet()){
                    for (Map.Entry<String, ScheduledItem> item: day.getValue().getScheduledItems().entrySet()) {
                        Log.d(TAG, item.getValue().getKeyItem());
                    }
                }

            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

检索第一张地图,但不检索第二张地图。它永远不会进入每个人的内心。

1 个答案:

答案 0 :(得分:0)

请使用此代码:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("trips").child(getUid());
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String tripName = dataSnapshot.child("tripName").getValue(String.class);
        String cityId = dataSnapshot.child("cityId").getValue(String.class);
        String arrivalDate = dataSnapshot.child("arrivalDate").getValue(String.class);
        String duration = dataSnapshot.child("duration").getValue(String.class);

        for(DataSnapshot ds : dataSnapshot.child("days").getChildren()) {
            String keyItem = ds.child("keyItem").getValue(String.class);
            String orderItem = ds.child("orderItem").getValue(String.class);
            String typeItem = ds.child("typeItem").getValue(String.class);
            Log.d("TAG", keyItem + " / " + orderItem + " / " + typeItem);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
uidRef.addListenerForSingleValueEvent(eventListener);

如果您从days节点中删除推送的密钥,则此代码将起作用。当只需要一个孩子时,有两个孩子。您可以删除当天的号码,比如说第1天,第2天,依此类推,按下键-KpMhSTQABFhbBHri0VU,-KpMhSTQABFhbBHri0AB。

您的日期节点应如下所示:

days={
    day1={
            keyItem=-KGYdK_RIc3Whm5Vp5WM, 
            orderItem=99, 
            typeItem=att,
    day2={
            keyItem=-KGYdK_RIc3Whm5Vp5WM, 
            orderItem=99, 
            typeItem=att,
    }
}

days={
    -KpMhSTQABFhbBHri0VU={
            keyItem=-KGYdK_RIc3Whm5Vp5WM, 
            orderItem=99, 
            typeItem=att,
    -KpMhSTQABFhbBHri0AB={
            keyItem=-KGYdK_RIc3Whm5Vp5WM, 
            orderItem=99, 
            typeItem=att,
    }
}