简体   繁体   中英

How to get an array list of images from Cloud FireStore into imageslider

I have saved images in Firestore in form of an ArrayList of type String which has its URL. I want to get those images stored in a field "images" into an imageslider that has Slidemodel that takes ArrayList as a parameter.

The class slideModel has the following variables:

private String imageUrl;
private Integer imagePath;
private ScaleTypes scaleTypes;
private String title;

The code pasted below is iterating over documents not the fields of a particular document

    db.collection("userimages").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {

            for (QueryDocumentSnapshot document : task.getResult()) 
                sliderDataArrayList.add(new SlideModel(document.getData().toString(), ScaleTypes.FIT));
                imageSlider.setImageList(sliderDataArrayList,ScaleTypes.FIT);
            }

        }

The image slider takes ArrayList as a parameter and a Scale Type.

This code in the image slider is getting the documents into the slider not the field of that document that contains the images. Image of how the data is structured in firestore

I want to get the "field": "images" which has the ArrayList of strings containing image URLs and then store it in the sliderDataArrayList.

final List<SlideModel> sliderDataArrayList = new ArrayList<>();

PLEASE SUGGEST TO ME A BETTER WAY TO GET AROUND IT OR AN ANSWER TO THIS PROBLEM THANK YOU!

According to your comment, because an answer for getting the data within the userimages array that exists in the NhVqAzmI6Cu9q8BTSMSr document, you say that it "will be really helpful", then here you are.

Because I cannot see any class that holds a List<String> , I will create one for you:

class DocumentClass {
    public List<String> images;
}

Now to get the desired array, we need to create a reference that points to the NhVqAzmI6Cu9q8BTSMSr document:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference userImagesRef = rootRef.collection("userimages");
DocumentReference docRef = userImagesRef.document("NhVqAzmI6Cu9q8BTSMSr");
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                DocumentClass documentClass = document.toObject(DocumentClass.class);
                List<String> images = documentClass.images;
                imageSlider.setImageList(images, ScaleTypes.FIT);
            } else {
                Log.d(TAG, "No such document");
            }
        } else {
            Log.d(TAG, "get failed with ", task.getException());
        }
    }
});

For more info, please check the following article:

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