java - 在 Android 应用程序的改造实现中访问 kotlin 类自动生成的 getter 方法

标签 java android kotlin

我正在尝试使用 kotlin 语言在 Android 应用程序中实现改造库,但我在尝试使用 kotlin 自动生成的 getter 功能来获取 MovieResponse 类的值。

这是 MovieResponse 类:

class MovieResponse {

    @SerializedName("page")
    private var page : Int? = null

    @SerializedName("results")
    private var results : List<Movie>? = null

    @SerializedName("total_results")
    private var totalResults : Int? = null

    @SerializedName("total_pages")
    private var totalPages : Int? = null

    constructor(page: Int?, results: List<Movie>?, totalResults: Int?, totalPages: Int?) {
        this.page = page
        this.results = results
        this.totalResults = totalResults
        this.totalPages = totalPages
    }


}

这是我的 android MainActivity 类:

class MainActivity : AppCompatActivity() {

    private val TAG : String = MainActivity::class.java.simpleName
    val BASE_URL : String = "http://api.themoviedb.org/3/"
    private var retrofit : Retrofit? = null
    private var recyclerView : RecyclerView? = null
    private var API_KEY : String = "166873e095bdb281691220d5ad12610c"

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

        // setup the layout manager
        recycler_view.setHasFixedSize(true)
        recycler_view.layoutManager = LinearLayoutManager(this)

        // get the data
        connectAndGetData()

    }

    /**
     * This method creates an instance of Retrofit
     * and set the base url
     */
    private fun connectAndGetData() {
        if (retrofit == null) {
            retrofit = Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
        }

        val movieApiService = retrofit!!.create(MovieApiService::class.java)

        val call : Call<MovieResponse> = movieApiService!!.getTopRatedMovies(API_KEY)
        call.enqueue(object : Callback<MovieResponse> {
            override fun onFailure(call: Call<MovieResponse>, t: Throwable) {
            }

            override fun onResponse(call: Call<MovieResponse>, response: Response<MovieResponse>) {
                val movies : List<Movie> = response.body() #<-- stuck here
                recyclerView!!.adapter = MoviesAdapter(movies, R.layout.list_item_movie, applicationContext)
                Log.d(TAG, "Number of movies received: " + movies.size)
            }
        })


    }
}

我无法得到这个

@SerializedName("results")
        private var results : List<Movie>? = null

从这里开始:

val movies : List<Movie> = response.body()

在普通的java中它是

val movies : List<Movie> = response.body().getResults()

但在 kotlin 中我似乎无法实现这一点。

我对“电影数据库 API”的回应:

{
    "page": 1,
    "total_results": 7444,
    "total_pages": 373,
    "results": [
        {
            "vote_count": 2080,
            "id": 19404,
            "video": false,
            "vote_average": 9,
            "title": "Dilwale Dulhania Le Jayenge",
            "popularity": 16.5,
            "poster_path": "\/uC6TTUhPpQCmgldGyYveKRAu8JN.jpg",
            "original_language": "hi",
            "original_title": "दिलवाले दुल्हनिया ले जायेंगे",
            "genre_ids": [
                35,
                18,
                10749
            ],
            "backdrop_path": "\/mMaxGuQKuH4WUHBwUNhJOetDYE9.jpg",
            "adult": false,
            "overview": "Raj is a rich, carefree, happy-go-lucky second generation NRI. Simran is the daughter of Chaudhary Baldev Singh, who in spite of being an NRI is very strict about adherence to Indian values. Simran has left for India to be married to her childhood fiancé. Raj leaves for India with a mission at his hands, to claim his lady love under the noses of her whole family. Thus begins a saga.",
            "release_date": "1995-10-20"
        },
        {...}
     ]
}

最佳答案

您的变量在 MovieResponse 类中似乎是私有(private)的,因此您无法从类外部访问它。检查this .

关于java - 在 Android 应用程序的改造实现中访问 kotlin 类自动生成的 getter 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57203920/

相关文章:

java - Android Nougat 中具有共享 Intent 的 NullPointerException

Java 继承 - 找不到在对象上调用的方法?

android - Android Studio 4.0.1,Gradle和React Native:存在时找不到AndroidManifest.XML

java - 按钮在第一个和最后一个索引处不起作用?

android - 构建已签名的 APK 失败

kotlin - 如何使用gradle kotlin dsl为简单的控制台应用程序生成 'fatjar'

java - 使用 if/else/if 语句从 double 返回值

带参数的 Java 字符串到 URI

android - 空指针异常-图像捕获

android - 如何在 Kotlin 中传递方法接受参数?