无法加载 Fragment kotlin 文件中函数的方法

发布于 2025-01-15 02:27:28 字数 1662 浏览 0 评论 0原文

当我尝试使用点运算符在片段 kotlin 文件方法中调用类的函数时,出现错误。

尝试调用 ImageGallery 类的 listOfImages 方法,

dataList=ImageGallery.listOfImages(context)

但这仅给出 @ listOfImages 单词的错误。

错误:未解析的参考:listOfImages

在片段中,在 loadImaged() 方法之外:

var dataList:ArrayList<String> = ArrayList()

用于从库中获取代码的类代码:

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
        }
    }

在片段活动中,

加载函数:

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+")")
    }

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+")")
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剪不断理还乱 2025-01-22 02:27:28

您需要创建一个 ImageGallery 实例才能使用它。换句话说,使用:

ImageGallery().listOfImages(context)

而不是

ImageGallery.listOfImages(context)

替代地,将函数放在 ImageGallery 的伴生对象中,其工作方式与 Java 中的静态函数类似,这样

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
        }   
    }
}

您就可以使用

ImageGallery.listOfImages(context)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文