dcdev
dcdev

Reputation: 1

Trying to Avoid Generics: "One type argument expected for class Result<out T>"

Trying to figure out what's going on...the solution described here doesn't seem to work for me: One type argument expected for class Result<out T>

I don't know if I need to adjust "Result" or whether there is some other issue.

Here's my spec for the data that is being fetched:

data class PopularMovies(
    val results: List<Result>
)

data class Result(
    val id: Int, val overview: String,
    val poster_path: String,
    val release_date: String,
    val title: String,
    val vote_average: Double,
    val vote_count: Int
)

Here's my code for MainActivity

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // ...

        fab.setOnClickListener { view ->
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show()
        }

        val request = ServiceBuilder.buildService(TmdbEndpoints::class.java)
        val call = request.getMovies(getString(R.string.api_key))

        call.enqueue(object : Callback<PopularMovies> {
            override fun onResponse(call: Call<PopularMovies>, response: Response<PopularMovies>) {
                if (response.isSuccessful){
                    progress_bar.visibility = View.GONE
                    recyclerView.apply {
                        setHasFixedSize(true)
                        layoutManager = LinearLayoutManager(this@MainActivity)
                        adapter = MoviesAdapter(response.body()!!)
                    }
                }
            }
            override fun onFailure(call: Call<PopularMovies>, t: Throwable) {
                Toast.makeText(this@MainActivity, "${t.message}", Toast.LENGTH_SHORT).show()
            }
        })
    }

    class MoviesAdapter(private val movies: PopularMovies): RecyclerView.Adapter<MoviesAdapter.MoviesViewHolder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MoviesViewHolder {
            val view = LayoutInflater.from(parent.context).inflate(R.layout.movie_item, parent, false)
            return MoviesViewHolder(view)
        }

        override fun getItemCount(): Int {
            return movies.results.size
        }

        override fun onBindViewHolder(holder: MoviesViewHolder, position: Int) {
            return holder.bind(movies.results[position])
        }

        class MoviesViewHolder(itemView : View): RecyclerView.ViewHolder(itemView){
            private val photo: ImageView = itemView.findViewById(R.id.movie_photo)
            private val title: TextView = itemView.findViewById(R.id.movie_title)
            private val overview:TextView = itemView.findViewById(R.id.movie_overview)
            private val rating:TextView = itemView.findViewById(R.id.movie_rating)

            // TODO: Figure out the issue with this code.
            // Following line gives error:
            // "One type argument expected for class Result<out T>"
            open fun bind(movie: Result) {
                Glide.with(itemView.context).load("http://image.tmdb.org/t/p/w500${movie.poster_path}").into(photo)
                title.text = "Title: "+movie.title
                overview.text = movie.overview
                rating.text = "Rating : "+movie.vote_average.toString()
            }

        }
    }
}

For reference, I'm trying to follow the tutorial here: https://dev.to/paulodhiambo/kotlin-and-retrofit-network-calls-2353

Any help is greatly appreciated - thank you!

Upvotes: 0

Views: 768

Answers (1)

Sergio
Sergio

Reputation: 30655

There is Result<out T> class in the Kotlin standard library, defined like this:

public inline class Result<out T> ...

I think you import this class instead of Result class defined by you. Please make sure you import your class in MainActivity:

import your.package.Result

Or you can change the name of Result class to Movie to avoid ambiguity.

Upvotes: 2

Related Questions