如何遍历List <object>或获取列表的元素?

时间:2017-04-11 17:50:52

标签: android list firebase firebase-realtime-database

我正在使用Firebase进行Android项目,我想获取有关条形图的信息,我想我正确地获取了数据但是我在数据库结构中有一些节点,比如该节点中的“开放时间”有信息喜欢打开和关闭小时,开始和结束时间的欢乐时光。我正在List<Object>中检索该节点,如下所示:

这是我的ValueEventListener

ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        // Get Post object and use the values to update the UI
        long value = dataSnapshot.getChildrenCount();
        Log.d("Bar", "No. of children: " + value);

        for (DataSnapshot districtSnapshot: dataSnapshot.getChildren()) {
            String district = districtSnapshot.getKey();

            for (DataSnapshot barSnapshot: dataSnapshot.child(district).getChildren()) {

                Bar bar = barSnapshot.getValue(Bar.class);
                String name = bar.getName();
                String address = bar.getAddress();
                List<String> photos = bar.getPhotos();
                List<Object> hours = bar.getHours();
                double latitude = bar.getLatitude();
                double longitude = bar.getLongitude();

                barsList.add(new Bar(name, address, latitude, longitude, photos));

                Log.d("Bar", "Lat: " + latitude);
                Log.d("Bar", "Long: " + longitude);
                Log.d("Bar", "Photos: " + photos.toString());
                Log.d("Bar", "Hours String: " + hours.toString());
            }
        }
    }

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

mPostReference.addListenerForSingleValueEvent(valueEventListener);

Log.d("Bar", "Hours String: " + hours.toString());我在数组中获得了正确的信息,但我不知道如何将其转换为我可以选择的方式,例如 happyStart

  

小时字符串:[{happyStart = 17,happyEnd = 19,open = 17,close = 2},   {happyStart = 17,happyEnd = 18,open = 16,close = 2}]

请帮帮我。提前谢谢!

更新,添加了Bar类:

@IgnoreExtraProperties
public class Bar {
    private String address;
    private String name;
    private double latitude;
    private double longitude;
    private List<Object> hours;
    private List<Object> beers;
    private List<String> photos;

    public Bar() { }

    public Bar(String name, String address, double latitude, double longitude, List<String> photos) {
        this.name = name;
        this.address = address;
        this.photos = photos;
    }

    @Exclude
    public Map<String, Object> toMap() {
        HashMap<String, Object> result = new HashMap<>();
        result.put("name", name);
        result.put("address", address);
        result.put("photos", photos);
        result.put("hours", hours);
        result.put("longitude", longitude);
        result.put("latitude", latitude);
        result.put("beers", beers);

        return result;
    }

    public String getName() {
        return name;
    }

    public String getAddress() {
        return address;
    }

    public List<Object> getHours() {
        return hours;
    }

    public List<Object> getBeers() {
        return beers;
    }

    public List<String> getPhotos() {
        return photos;
    }

    public double getLatitude() {
        return latitude;
    }

    public double getLongitude() {
        return longitude;
    }
}

2 个答案:

答案 0 :(得分:3)

尝试使用

private List<Map<String, Integer>> hours;

然后,它将是

int happyStart = beer.getHours().get(0).get("happyStart");
添加

.get(0)以获取列表中的第一个。

您也可以尝试

for (Map<String, Integer> hours : beer.getHours()) {
    Log.d("happyStart", "" + hours.get("happyStart"));
}

或者,您需要继续将Firebase结构映射到Java对象以获得Hour类。

答案 1 :(得分:0)

创建另一个名为Hours

的类
public class Hours {
    private String happyStart;
    private String happyEnd;
    private String open;
    private String close;
}

添加Bar类之类的所有getter和setter,然后将private List<Object> hours;替换为private List<hours> hours;

然后,您可以从List对象

中检索您喜欢的任何小时数