0

is there a class that both IntArray abd StringArray heritate from i have a function that will need as argument all sort of Arrays to use it in a fragmentadapter

class MyPagerAdapter(val fragmentManager: FragmentManager,val collection:Array):FragmentStatePagerAdapter(fragmentManager){
override fun getCount(): Int {
    Log.i("viewpager","count"+fragmentManager.fragments.size.toString())

     return collection.size
    }
}

is it possible ? or would i use another way ?

2 Answers 2

3

If you want to handle any Array<T> type, your class should probably be generic:

class MyPagerAdapter<T>(val fragmentManager: FragmentManager, val collection: Array<T>)

There is no common supertype for the primitive array wrappers, there are however toTypedArray functions or each of them that can convert them to the appropriately typed generic array instance, for example:

val intArray: IntArray = intArrayOf(1, 2, 3)
val typedArray: Array<Int> = intArray.toTypedArray()
MyPagerAdapter(fragmentManager, typedArray)
Sign up to request clarification or add additional context in comments.

2 Comments

Well elaborate.
that's why i need exactly
1

If you only need collection.size, just pass the size.

If you need anything else, it can't be done generically and handle IntArray and other primitive arrays; you'll need to convert to Array<T> as in @zsmb13's answer.

1 Comment

i need all the content of the array , not just the size , so i think working with the generic class is the correct way

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.