0

MoviesData.java is a Java class. I'm trying to pass it to another class from MainActivity.kt to MovieRequest.kt

I'm trying to refactor and have very little idea about what I'm doing.

Here is MoviesData.java

public class MoviesData {

@NotNull
public final List<? extends Result> results;

public MoviesData(List<? extends Result> results) {
    this.results = results;
}
}

This is the piece in MainActivity.kt that is probably wrong:

val movieType = MoviesData( *****results:***** I don't know what to put here *****)
    MovieRequest(movieType)

This is MovieRequest.kt. Note that all of the instances of movieType in this file are showing errors, so there's likely something wrong here as well:

open class MovieRequest(movieType: Array<String>) : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

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

    call.enqueue(object : Callback<movieType> {
        override fun onResponse(call: Call<movieType>, response: Response<movieType>) {
            if (response.isSuccessful) {
                progress_bar.visibility = View.GONE
                recyclerView.apply {
                    setHasFixedSize(true)
                    layoutManager = LinearLayoutManager(this@MovieRequest)
                    adapter = MoviesAdapter(response.body()!!.results as List<Result>)
                }
            }
        }

        override fun onFailure(call: Call<MoviesData>, t: Throwable) {
            Toast.makeText(this@MovieRequest, "${t.message}", Toast.LENGTH_SHORT).show()
        }
    })
}

}

petestmart
  • 500
  • 11
  • 27
  • To make it compile, put `val` in front of the constructor parameter so it also becomes a property. But this won't work at runtime, because Android requires an empty constructor for Activities. The recommended design nowadays is to use a single Activity and multiple Fragments. They can share data through a common ViewModel. But if you still want to use two Activities, you need to pass the object through a Bundle in the Intent extras of the Intent used to launch the second Activity. https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application – Tenfour04 Nov 03 '21 at 18:17
  • Note your class needs to be Serializable or Parcelable to be able to put it in a Bundle. You can look up how to do this in Kotlin. Parcelable is preferable and easily done using the `kotlin-parcelize` plugin. – Tenfour04 Nov 03 '21 at 18:21

0 Answers0