0

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!

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
ritik raj
  • 3
  • 2
  • So you need to get all URLs from all documents? – Alex Mamo Jun 03 '21 at 09:10
  • i want to get all URLs from one document into imageslider ..is there any other way to display multiple images if u can provide an article or a documentation or source from where i can implement that. i have provided an image from firestore how the images are stored – ritik raj Jun 03 '21 at 10:52
  • You say "from one document", which one? The one that exists at `root -> userimages -> NhVq ... SMSr`? Have stored the document id in a variable? – Alex Mamo Jun 03 '21 at 10:58
  • Yes from that document"NhVq....SMSr" and no I have not stored document Id in a variable – ritik raj Jun 03 '21 at 11:11
  • Is it ok for you if I provide you answer using the hardcoded document id? – Alex Mamo Jun 03 '21 at 11:26
  • I'm not sure I understand. What would you like to achieve? – Alex Mamo Jun 03 '21 at 11:45
  • I am sorry for confusing you can just help with the problem I asked earlier to display images of a particular document with with hardcoded document Id – ritik raj Jun 03 '21 at 11:49
  • Is it helpful for you, to have a solution that can help you get the array of "NhVq....SMSr" (single) document and send it to imageSlider? – Alex Mamo Jun 03 '21 at 12:36
  • Yes sir that would be really helpful thank you – ritik raj Jun 03 '21 at 12:36

1 Answers1

0

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:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • This answer seems to work but throws error ' Can't convert object of type java.lang.String to type com.denzcoskun.imageslider.models.SlideModel ' – ritik raj Jun 03 '21 at 18:36
  • Most likely you have defined the list to be of type `SlideModel`, which is not correct. You should define it as String, as in my above answer. Try to change it and tell me if it works. – Alex Mamo Jun 03 '21 at 18:38
  • if i change it to List the imageslider.setimageList takes only List as a parameter and the file imageSlider and slideModel are a read-only file downlaoded with a dependency "implementation 'com.github.denzcoskun:ImageSlideshow:0.1.0'" – ritik raj Jun 03 '21 at 18:54
  • your answer might work if there was a way to edit these read only files or an alternate that i think is to make another imageslider class/adapter which will take List as a parameter . Also thanks for helping this much i will upvote this since the answer is right but cant apply it – ritik raj Jun 03 '21 at 18:57
  • if u still think there is a way this can work let me know or some source with imageSlider with firestore – ritik raj Jun 03 '21 at 19:03
  • You might consider changing imageslider to take a List, or, change the array in the database to hold SlideModel objects, and not Strings as it holds now, right? – Alex Mamo Jun 03 '21 at 19:05
  • Imageslider is a read-only file so I can't change it to take a list and can't tell database object to hold slidemodel objects cause it has 4 parameters – ritik raj Jun 03 '21 at 19:25
  • Change the array in the database to hold SlideModel object instead of Strings and then use the solution in the linked article, ok? – Alex Mamo Jun 03 '21 at 19:38
  • Or convert the List that I showed you in my answer to a List, and your problem will be solved. – Alex Mamo Jun 03 '21 at 19:47
  • Hey, ritik raj! Is there everything alright? Have you solved the issue? – Alex Mamo Jun 04 '21 at 06:25
  • I am getting the field values in log but can put it in a imageslider I am searching for an alternative to this imageslider I will definitely let you know if this works – ritik raj Jun 04 '21 at 09:17
  • Fair enough, keep me posted. – Alex Mamo Jun 04 '21 at 09:37
  • the new error "showImageActivity$DocumentClass does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped" Even the SlideModel class does not hold a List – ritik raj Jun 06 '21 at 08:45
  • i have made this ' final List sliderDataArrayList = new ArrayList<>();' to pass this arraylist in imagesliderof type slidemodel but how do i add the documentsnapshot.toObject(SLidemodel.class) into this arraylist – ritik raj Jun 06 '21 at 08:58
  • i have tried using this method too [https://origin.geeksforgeeks.org/how-to-create-dynamic-auto-image-slider-in-android-with-firebase/] but this one getting one image from one document – ritik raj Jun 06 '21 at 09:55
  • hey ALex, please help me out i really dont know what to do i am uploading the whole file on this github repo please check it out [https://github.com/sahilzilka/imageslider/tree/main/mymallapp – ritik raj Jun 06 '21 at 10:06
  • For that error, check this [answer](https://stackoverflow.com/a/51036439/5246885). – Alex Mamo Jun 06 '21 at 10:30
  • If you get new errors, please post a new question using its own [MCVE](https://stackoverflow.com/help/mcve), so I and other Firebase developers can help you. – Alex Mamo Jun 06 '21 at 10:30
  • The slidemodel only takes a single string as a parameter if there was a way to add each string URL one by one into slidemodel array list then I could pass that array list into setimagelist....or assign the whole images[ ] array list from firestore directly as list into the slidemodel array list then pass it to the setimageList – ritik raj Jun 07 '21 at 05:01
  • It did at some point so yeah thanks a lot I will be reposting this answer I hope u and other developers can help me – ritik raj Jun 08 '21 at 12:17
  • hey can u help me with one more thing can u tell me atleast how to log these images in form of array list – ritik raj Jun 10 '21 at 06:25
  • `List images = documentClass.images;` is an ArrayList. So if you only want to Log them, simply pass images to a Log statement. `Log.d("TAG", images.toString());`. – Alex Mamo Jun 10 '21 at 06:36