我如何从该Firebase JSON数据库获取所有图像

时间:2018-08-22 19:09:10

标签: java android firebase firebase-realtime-database

JSON Image Link

我想使用此代码从我的Firebase数据库中获取所有图像

FirebaseDatabase mFirebaseInstance = FirebaseDatabase.getInstance();


    mFirebaseInstance.getReference("actors").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

2 个答案:

答案 0 :(得分:0)

您最好使用addListenerForSingleValueEvent而不是addValueEventListener,因为它将被调用一次,因此非常有帮助,请尝试下面的代码

ArrayList<String> arr_imageList = new ArrayList<>();


DatabaseReference reference = FirebaseDatabase.getInstance().getReference();

reference.child("actors").addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        arr_imageList.clear();
        if (dataSnapshot.exists()) {

            for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {

                ActorsModel actorsModel = dataSnapshot1.getValue(ActorsModel.class);

                if (actorsModel != null && actorsModel.getImage() != null) {

                    arr_imageList.add(actorsModel.getImage());
                }
            }

        }

    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

并请在下面添加ActorsModel的模型类

public class ActorsModel implements Serializable {

    private String children;
    private String country;
    private String description;
    private String dob;
    private String height;
    private String image;
    private String name;
    private String spouse;

    public ActorsModel() {
    }

    public ActorsModel(String children, String country, String description, String dob, String height, String image, String name, String spouse) {
        this.children = children;
        this.country = country;
        this.description = description;
        this.dob = dob;
        this.height = height;
        this.image = image;
        this.name = name;
        this.spouse = spouse;
    }

    public String getChildren() {
        return children;
    }

    public void setChildren(String children) {
        this.children = children;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getDob() {
        return dob;
    }

    public void setDob(String dob) {
        this.dob = dob;
    }

    public String getHeight() {
        return height;
    }

    public void setHeight(String height) {
        this.height = height;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSpouse() {
        return spouse;
    }

    public void setSpouse(String spouse) {
        this.spouse = spouse;
    }
}

答案 1 :(得分:0)

要解决此问题,您需要使用DataSnapshot方法遍历getChildren()对象。因此,请使用以下代码行:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query query = rootRef.child("actors").orderByChild("image");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String image = ds.child("image").getValue(String.class);
            Log.d(TAG, image);
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage());
    }
};
query.addListenerForSingleValueEvent(valueEventListener);

logcat中的输出将是所有图像URL。