I am doing a project and the only help I have are in java, and it is making it very difficult for me to have to convert it or pass it to kotlin this is the code I have to pass to kotlin:
private class ScreenSliderPagerAdapter extends FragmentStatePagerAdapter { public ScreenSliderPagerAdapter(@NonNull FragmentManager fm) { super(fm); } @NonNull @Override public Fragment getItem(int position) { switch(position) { case 0: SwipeFragement1 tab1 = new SwipeFragement1(); return tab1; case 1: SwipeFragement2 tab2 = new SwipeFragement2(); return tab2; case 2: SwipeFragemen3 tab3 = new SwipeFragement3(); return tab3; } return null; } @Override public int getCount() { return 0: } }
and when trying to convert it, it was more or less like this:
private class ScreenSliderPagerAdapter (fm : FragmentManager @NonNull) : FragmentStatePagerAdapter { var fm : FragmentManager = fm constructor():this() { super(fm) } @NonNull @Override public fun getItem(position: Int): SwipeFragement1? { var tab : SwipeFragement1? = null when(position) { 0 -> tab = SwipeFragement1() 1 -> tab = SwipeFragement2() 2 -> tab = SwipeFragement3() } return tab } @Override public fun getCount(): Int { return 0 } }
According to how little I know, Kotlin is looking very bad as seen in the following screenshot:
In case you’re wondering I’m trying to fragment an actity
Advertisement
Answer
You got that error because the Kotlin code has some syntax errors. Here is the right one.
private class ScreenSliderPagerAdapter(fm: FragmentManager) : FragmentStatePagerAdapter(fm) { val NUM_TABS = 3 override fun getItem(position: Int): Fragment { return when (position) { 0 -> SwipeFragement1() 1 -> SwipeFragement2() else -> SwipeFragement3() } } override fun getCount(): Int { return NUM_TABS } }