0

Please do not mark it as 'duplicate' and Close the question simply by attaching any other question. My context is different from the suggested answer. I need an answer for Kotlin, I want to display images in an Image Slider on recyclerView. Thanks for the understanding and not for closing it. At least others who are willing to help can help me with this. I have tried myself a lot, more than a week, refer a lot of tutorials, videos on YouTube and after that only I asked here on Stackoverflow.

I wanted to add an image carousel/image slide in my app and I have been trying to do it for more than a week.

I always failed in getting the array of images (SlideModel). Every time I check the size of my array, I found it's zero. I know it's because of the time taken to fetch details from the Firestore and I do not know how to handle it. I went through many tutorials including YouTube videos on Image Slide, but their scenario is different from mine. I have the imageslider in my recyclerView and my images are in the Firebase Storage and the links are in the Firestore and I am using Kotlin. I have managed to show the images in an ImageView, fetch other details and show them in the recyclerView. I could also show the images in the `imageSlider when the images are in the drawable.

I tried using the following image slider.

implementation 'com.github.denzcoskun:ImageSlideshow:0.1.0'

I tried many different ways, but nothing really makes sense and that is why I am not adding my code here. Thanks in advance for your help.

Edit:

I have function getImagesForSlider() in my DashboardFragment.kt that will be called from the DashboardItemsListAdapter.kt

class DashboardFragment : BaseFragment() {

    val remoteImages = ArrayList<SlideModel>()

    private lateinit var binding: FragmentDashboardBinding

   fun getImagesForSlider(productId: String): ArrayList<SlideModel> {

        Log.d("Tag", "Called getImagesForSlider function")
        Log.d("Tag", "product id is $productId")

        val mFireStore = FirebaseFirestore.getInstance()
        mFireStore.collection("products")
            .document(productId)
            .get()
            .addOnSuccessListener { document ->

                val numOfImages= document.getLong("image_count")?.toInt()
                Log.d("Tag", "Number of images is $numOfImages")

                when {
                    numOfImages == 1 -> {

                        remoteImages.add(
                            SlideModel(
                                document.getString("image"),
                                document.getString("title"),
                                ScaleTypes.FIT
                            )
                        )
                    }
                    numOfImages == 2 -> {
                        
                        remoteImages.add(
                            SlideModel(
                                document.getString("image1"),
                                document.getString("title"),
                                ScaleTypes.FIT
                            )
                        )

                        remoteImages.add(
                            SlideModel(
                                document.getString("image2"),
                                document.getString("title"),
                                ScaleTypes.FIT
                            )
                        )
                    }
                    numOfImages == 3 -> {

                        remoteImages.add(
                            SlideModel(
                                document.getString("image1"),
                                document.getString("title"),
                                ScaleTypes.FIT
                            )
                        )

                        remoteImages.add(
                            SlideModel(
                                document.getString("image2"),
                                document.getString("title"),
                                ScaleTypes.FIT
                            )
                        )

                        remoteImages.add(
                            SlideModel(
                                document.getString("image3"),
                                document.getString("title"),
                                ScaleTypes.FIT
                            )
                        )
                    }
                    numOfImages == 4 -> {
                       
                        remoteImages.add(
                            SlideModel(
                                document.getString("image1"),
                                document.getString("title"),
                                ScaleTypes.FIT
                            )
                        )

                        remoteImages.add(
                            SlideModel(
                                document.getString("image2"),
                                document.getString("title"),
                                ScaleTypes.FIT
                            )
                        )

                        remoteImages.add(
                            SlideModel(
                                document.getString("image3"),
                                document.getString("title"),
                                ScaleTypes.FIT
                            )
                        )

                        remoteImages.add(
                            SlideModel(
                                document.getString("image4"),
                                document.getString("title"),
                                ScaleTypes.FIT
                            )
                        )
                       
                    }
                    numOfImages == 5 -> {
                        remoteImages.add(
                            SlideModel(
                                document.getString("image1"),
                                document.getString("title"),
                                ScaleTypes.FIT
                            )
                        )

                        remoteImages.add(
                            SlideModel(
                                document.getString("image2"),
                                document.getString("title"),
                                ScaleTypes.FIT
                            )
                        )

                        remoteImages.add(
                            SlideModel(
                                document.getString("image3"),
                                document.getString("title"),
                                ScaleTypes.FIT
                            )
                        )

                        remoteImages.add(
                            SlideModel(
                                document.getString("image4"),
                                document.getString("title"),
                                ScaleTypes.FIT
                            )
                        )

                        remoteImages.add(
                            SlideModel(
                                document.getString("image5"),
                                document.getString("title"),
                                ScaleTypes.FIT
                            )
                        )
                    }
                    else -> {
                        Log.d("Tag", "There are no images")
                    }
                }

            }
       
        return remoteImages
    }
}

Following is the part of my DashboardItemsListAdapter.kt where the above function will be called.

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            val model = list[position]
    
            if (holder is MyViewHolder) {
    
                val remoteImages =DashboardFragment().getImagesForSlider(model.product_id)
holder.itemView.image_slider.setImageList(remoteImages,ScaleTypes.FIT)

I don't know if my entire code is a blunder.

My database is as below.

enter image description here

Codist
  • 737
  • 8
  • 23
  • Please edit your question and show us what you have tried. – Alex Mamo Jul 20 '21 at 06:05
  • are you able to fetch `image urls` at all? If yes, then this is an issue related to handling the network call where data is loading after the `recyclerview` has been set. Post the code where you get the `image urls` from firebase storage, and after that I or someone can tell you how to set it to the adapter, probably you will be using some `listener` from firebase :) – mehul bisht Jul 20 '21 at 07:06
  • I have added code to my question. Thanks in advance the help. – Codist Jul 21 '21 at 07:49
  • @AlexMamo Could you check help? – Codist Jul 22 '21 at 04:48
  • There is no way you can return the `remoteImages` as a result of a method. Any code that needs data from the database, needs to be inside the onComplete method, or be called from there. Firebase API is asynchronous. So please check the duplicate to see how can you solve this using a custom callback. – Alex Mamo Jul 22 '21 at 04:50

0 Answers0