I need to put an kotlin.collections.ArrayList
into Android Bundle whitch reqire java.util.ArrayList
NOTE: I cannot Parcelize/Serialize
If I could Parcelize I would do somthing like this
val transactionList: kotlin.collections.ArrayList<Object> = arrayListOf()
val bundle = Bundle() // Require java.util.ArrayList
bundle.putParcelableArrayList("transactions", transactionList as ArrayList<out Parcelable>)
Is there another way to pass a List to another fragment?
CodePudding user response:
One option would be to save the list in a ViewModel that both fragments share.
CodePudding user response:
You can easily create a new ArrayList from any Collection
val transactionList: kotlin.collections.ArrayList<Object> = arrayListOf()
val arrayList:java.util.ArrayList = ArrayList(transactionList)
val bundle = Bundle()
bundle.putParcelableArrayList("transactions", arrayList)
CodePudding user response:
ArrayList is a class with implementation details. FileUtils is returning a LinkedList and that is why you are having issues. You should do a cast to List instead since it is an interface that both ArrayList and LinkedList implement.
If you would need to modify the returned list you can use MutableList instead the immutable one.
But this is not the best approach because if the creators of FileUtils later change the implementation details of their collections your code may start crashing.
A better soulition if explicitly need it to be an arrayList instead of a generic list could be:
val myFiles = FileUtils.listFiles(mediaStorageDir, extensions, true)
val array = arrayListOf<File>()
array.addAll(myFiles)