Ritvik
Ritvik

Reputation: 33

findViewById error - Not enough information to infer type variable T. I copied java code and converted it online

Copied java code from diolor/swipecards (GITHUB). Converted it into kotlin with help pf online tools. It had some errors which were corrected but this last one still appears (findViewById) in the OnScroll function.

package com.example.fanatic

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.ArrayAdapter
import android.widget.Toast
import com.lorentzos.flingswipe.SwipeFlingAdapterView

class Swipe_card_activity : AppCompatActivity() {


    private var al:ArrayList<String> = TODO()
    private lateinit var arrayAdapter:ArrayAdapter<String>
    private var i:Int = 0

    override fun onCreate(savedInstanceState:Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_swipe_card_activity)
        al = ArrayList()
        al.add("php")
        al.add("c")
        al.add("python")
        al.add("java")
        al.add("html")
        al.add("c++")
        al.add("css")
        al.add("javascript")


        arrayAdapter = ArrayAdapter(this, R.layout.item, R.id.helloText, al)

        val flingContainer : SwipeFlingAdapterView = findViewById<SwipeFlingAdapterView>(R.id.frame)

        flingContainer.setAdapter(arrayAdapter)
        flingContainer.setFlingListener(object: SwipeFlingAdapterView.onFlingListener {
            override fun removeFirstObjectInAdapter() {
                // this is the simplest way to delete an object from the Adapter (/AdapterView)
                Log.d("LIST", "removed object!")
                al.removeAt(0)
                arrayAdapter.notifyDataSetChanged()
            }
            override fun onLeftCardExit(dataObject:Any) {
                //Do something on the left!
                //You also have access to the original object.
                //If you want to use it just cast it (String) dataObject
                makeToast(this@Swipe_card_activity, "Left!")
            }
            override fun onRightCardExit(dataObject:Any) {
                makeToast(this@Swipe_card_activity, "Right!")
            }
            override fun onAdapterAboutToEmpty(itemsInAdapter:Int) {
                // Ask for more data here
                al.add("XML " + (i).toString())
                arrayAdapter.notifyDataSetChanged()
                Log.d("LIST", "notified")
                i++
            }

THE ERROR IS PRESENT HERE ON FINDVIEWBYID IT SAYS : NOT ENOUGH INFORMATION TO INFER TYPE T.

            **override fun onScroll(scrollProgressPercent:Float) {
               strong textval viuw = flingContainer.selectedView
                viuw.run {
                    findViewById(R.id.item_swipe_right_indicator).setAlpha(if (scrollProgressPercent < 0) -scrollProgressPercent else 0)
                    findViewById(R.id.item_swipe_left_indicator).setAlpha(if (scrollProgressPercent > 0) scrollProgressPercent else 0)**
                }
            }
        })
        // Optionally add an OnItemClickListener
        flingContainer.setOnItemClickListener { itemPosition, dataObject -> makeToast(this@Swipe_card_activity, "Clicked!") }
    }
    fun makeToast(ctx: Context, s:String) {
        Toast.makeText(ctx, s, Toast.LENGTH_SHORT).show()
    }
    @OnClick(R.id.right)
    fun right() {
        /**
         * Trigger the right event manually.
         */
        val flingContainer : SwipeFlingAdapterView = findViewById<SwipeFlingAdapterView>(R.id.frame)
        flingContainer.getTopCardListener().selectRight()
    }
    @OnClick(R.id.left)
    fun left() {
        val flingContainer : SwipeFlingAdapterView = findViewById<SwipeFlingAdapterView>(R.id.frame)
        flingContainer.getTopCardListener().selectLeft()
    }

}

annotation class OnClick(val right: Int)

Upvotes: 3

Views: 877

Answers (2)

David Kroukamp
David Kroukamp

Reputation: 36423

The type you are getting with findViewById is potentially unconstrained so the type cannot be inferred and in Kotlin needs to be explicitly stated/casted.

If you are targeting API 26 or higher in your app you can do:

findViewById<THE_VIEW_TYPE>(R.id.item_swipe_right_indicator).setAlpha(...)

Otherwise for API 25 and lower you can do:

(findViewById(R.id.item_swipe_right_indicator) as THE_VIEW_TYPE).setAlpha(...)

Upvotes: 2

Saksham Pruthi
Saksham Pruthi

Reputation: 424

I believe the problem you are facing due to code conversion. Java doesn't require you to cast the view explicitly whereas Kotlin requires you to specify the type of view. You need to set the view within angular brackets like this

findViewById<View>(R.id.item_swipe_right_indicator).setAlpha(...)

Upvotes: 4

Related Questions