Home > front end >  Couldn't load the method of the function in Fragment kotlin file
Couldn't load the method of the function in Fragment kotlin file

Time:03-18

When I am trying to call the function of a class in a fragment kotlin file method using dot operator, it is giving the error.

Trying to call the listOfImages method of ImageGallery class as,

dataList=ImageGallery.listOfImages(context)

But this is giving errors @ listOfImages word only.

Error: Unresolved reference: listOfImages

In the fragment,outside the loadImaged() method:

var dataList:ArrayList<String> = ArrayList()

Class Code for Fetching the code from gallery:

public class ImageGallery {
    public fun listOfImages(context: Context) : ArrayList<String> {
        var imageList: ArrayList<String> = ArrayList()
        var projection = arrayOf(MediaStore.MediaColumns.DATA,MediaStore.Images.Media.BUCKET_DISPLAY_NAME)
        var orderBy:String=MediaStore.Video.Media.DATE_TAKEN

            val imagecursor: Cursor? = context.contentResolver.query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null,
                null, orderBy "DESC"
            )
            for (i in 0 until imagecursor!!.count) {
                imagecursor.moveToPosition(i)
                val dataColumnIndex =
                    imagecursor.getColumnIndex(MediaStore.Images.Media.DATA)
                imageList.add(imagecursor.getString(dataColumnIndex))
            }
            return imageList
        }
    }

In the fragment activity,

The load function:

private fun loadImage(){
        recyclerView.setHasFixedSize(true)
        recyclerView.layoutManager=GridLayoutManager(context,3)

        dataList=ImageGallery.listOfImages(context)

        adapterr= context?.let { CustomAdapter(it,dataList) }
        recyclerView.adapter=adapterr
        galleryNumber?.text=("Photos ("  dataList.size ")")
    }

CodePudding user response:

You need to create an instance of ImageGallery to use it. In other words, use:

ImageGallery().listOfImages(context)

instead of

ImageGallery.listOfImages(context)

Alternatively, put the function in a companion object of ImageGallery which works similarly like static functions in Java, like

public class ImageGallery {
    
    companion object {
        public fun listOfImages(context: Context) : ArrayList<String> {
            var imageList: ArrayList<String> = ArrayList()
            var projection = arrayOf(MediaStore.MediaColumns.DATA,MediaStore.Images.Media.BUCKET_DISPLAY_NAME)
            var orderBy:String=MediaStore.Video.Media.DATE_TAKEN

            val imagecursor: Cursor? = context.contentResolver.query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null,
                null, orderBy "DESC"
            )
            for (i in 0 until imagecursor!!.count) {
                imagecursor.moveToPosition(i)
                val dataColumnIndex =
                    imagecursor.getColumnIndex(MediaStore.Images.Media.DATA)
                imageList.add(imagecursor.getString(dataColumnIndex))
            }
            return imageList
        }   
    }
}

this allows you to use

ImageGallery.listOfImages(context)
  • Related