无法加载 Fragment kotlin 文件中函数的方法
当我尝试使用点运算符在片段 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要创建一个
ImageGallery
实例才能使用它。换句话说,使用:而不是
替代地,将函数放在
ImageGallery
的伴生对象中,其工作方式与 Java 中的静态函数类似,这样您就可以使用
You need to create an instance of
ImageGallery
to use it. In other words, use:instead of
Alternatively, put the function in a companion object of
ImageGallery
which works similarly like static functions in Java, likethis allows you to use