Home > OS >  Pull information from the main api model
Pull information from the main api model

Time:08-16

there is a question, how to pull the genre name from Movie Details to put it separately in textview?

data class MovieDetails(
    val adult: Boolean,
    val backdrop_path: String,
    val belongs_to_collection: Any,
    val budget: Int,
    val genres: List<Genre>,
    val homepage: String,
    val id: Int,
    val imdb_id: String,
    val original_language: String,
    val original_title: String,
    val overview: String,
    val popularity: Double,
    val poster_path: Any,
    val production_companies: List<ProductionCompany>,
    val production_countries: List<ProductionCountry>,
    val release_date: String,
    val revenue: Int,
    val runtime: Int,
    val spoken_languages: List<SpokenLanguage>,
    val status: String,
    val tagline: String,
    val title: String,
    val video: Boolean,
    val vote_average: Double,
    val vote_count: Int
)
data class Genre(
    val id: Int,
    val name: String
)
class MovieDetail : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_movie_detail)
        getMovieById()
        }
    fun getMovieById(){
        val id = intent.getIntExtra("id", 0)
        val apiInterface = id?.let { ApiInterface.create().getMovieById(it, "api_key") }
        GlobalScope.launch(Dispatchers.IO) {
            apiInterface?.enqueue(object : Callback<MovieDetails>{
                override fun onResponse(call: Call<MovieDetails>, response: Response<MovieDetails>) {
                    tvMovieName.text = response?.body()?.original_title.toString()
                    tvRating.text = response?.body()?.vote_average.toString()
                    tvRelease.text = response?.body()?.release_date.toString()
                    tvOverview.text = response?.body()?.overview.toString()
                    tvGenres.text = ???
                    Picasso.get().load("https://image.tmdb.org/t/p/w500"   response?.body()?.backdrop_path).into(ivPoster)
                }
                override fun onFailure(call: Call<MovieDetails>, t: Throwable) {
                    Toast.makeText(applicationContext, "Error $t", Toast.LENGTH_LONG).show()
                }
            })
        }
    }
}

tried to make an apiInterface?.enqueue(object : Callback separately, but failed and I can't figure out how to get the data out of the API

CodePudding user response:

If you need to update text with comma separated names you can do like this

tvGenres.text = response?.body()?.genres?.map {it.name}.joinToString(",")?:""

If you need to update text with genere namelist coming in new lines,you can do this.

response?.body()?.genres?.forEach {
tvGenres.append("${it.name}\n")
}
  • Related