简体   繁体   中英

Needed suggestion for proper conversion from Java to Kotlin

Android Studio has an automated Java-to-Kotlin converter. On some cases, it doesn't work smoothly, which means some manual work is needed.

Here's an example:

private TextView[] dots;

 private void addBottomDots(int currentPage) {
        dots = new TextView[layouts.length];

        int[] colorsActive = getResources().getIntArray(R.array.array_dot_active);
        int[] colorsInactive = getResources().getIntArray(R.array.array_dot_inactive);

        dotsLayout.removeAllViews();
        for (int i = 0; i < dots.length; i++) {
            dots[i] = new TextView(getContext());
            dots[i].setText(Html.fromHtml("&#8226;"));
            dots[i].setTextSize(35);
            dots[i].setTextColor(colorsInactive[currentPage]);
            dotsLayout.addView(dots[i]);
        }

        if (dots.length > 0)
            dots[currentPage].setTextColor(colorsActive[currentPage]);
    }

The result of auto-conversion + manual tweaking is:

private var dots: Array<TextView?>? = null  

    private fun addBottomDots(currentPage: Int) {
        dots = arrayOfNulls(layouts!!.size)

        val colorsActive = getResources().getIntArray(R.array.array_dot_active)
        val colorsInactive = getResources().getIntArray(R.array.array_dot_inactive)

        dotsLayout!!.removeAllViews()
        for (i in dots!!.indices) {
            dots[i] = TextView(getContext()) // part A
            dots!![i].text = Html.fromHtml("&#8226;") // part B
            dots!![i].textSize = 35f
            dots!![i].setTextColor(colorsInactive[currentPage])
            dotsLayout!!.addView(dots!![i])
        }

        if (dots!!.size > 0)
            dots!![currentPage].setTextColor(colorsActive[currentPage])
    }   

At part A, Android studio gives this error message:

Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type TextView?

And at part B:

Smart cast to 'Array' is impossible, because 'dots' is a mutable property that could have been changed by this time.

Another case:

public class MyViewPagerAdapter extends PagerAdapter {
        private LayoutInflater layoutInflater;

        public MyViewPagerAdapter() {
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View view = layoutInflater.inflate(layouts[position], container, false);
            container.addView(view);

            return view;
        }
}

which converted into:

inner class MyViewPagerAdapter : PagerAdapter() {
        private var layoutInflater: LayoutInflater? = null

        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            layoutInflater = getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE) // part C

            val view = layoutInflater!!.inflate(layouts!![position], container, false)
            container.addView(view)

            return view
        }

At part C:

Type mismatch. Required: LayoutInflater? Found: Any!

How to fix this?

Try the following

private var dots: ArrayList<TestView> = ArrayList()

private fun addBottomDots(currentPage: Int) {
    val size = layouts?.size ?: 0

    val colorsActive = getResources().getIntArray(R.array.array_dot_active)
    val colorsInactive = getResources().getIntArray(R.array.array_dot_inactive)

    dotsLayout?.removeAllViews()

    for (i in 0 until size) {
        val textView = TextView(getContext()) // part A
        textView.text = Html.fromHtml("&#8226;") // part B
        textView.textSize = 35f
        textView.setTextColor(colorsInactive[currentPage])
        dots.add(textView)
        dotsLayout?.addView(dots[i])
    }

    if (dots.size > 0)
        dots[currentPage].setTextColor(colorsActive[currentPage])
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM