I'm pretty new. I'm working on a project for a job interview. I've written the project in Kotlin, but as a requirement I need to have one file in Java.
I decided to convert a constructor class that receives a list of movie data from an API from Kotlin to Java. Now I'm not sure how to pass the list into it.
Kotlin:
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
)
to Java:
import org.jetbrains.annotations.NotNull;
import java.util.List;
public class PopularMovies {
@NotNull
public final Object results;
public PopularMovies() {
results = null;
}
class Result {
private int id;
private String overview;
private String poster_path;
private String release_date;
private String title;
private Double vote_average;
private int vote_count;
public Result(
int id,
String overview,
String poster_path,
String release_date,
String title,
Double vote_average,
int vote_count) {
this.id = id;
this.overview = overview;
this.poster_path = poster_path;
this.release_date = release_date;
this.title = title;
this.vote_average = vote_average;
this.vote_count = vote_count;
}
}
}
The problem I'm running into is when I hit the adapter:
class MoviesAdapter(val movies: List<Result>): RecyclerView.Adapter<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)
} ...
There is a red line under List<Result>
with an error:
One type argument expected for class Result<out T>
kotlin Result.class
@JvmInline
@SinceKotlin
public final value class Result<out T> : Serializable /* = Serializable */
CodePudding user response:
The Kotlin type List<Result>
is similar to List<? extends Result>
in Java, although in Java it would be common for succinctness to just put List<Result>
if you don't need to handle covariance. Either way, it's not exactly the same because the Java version exposes methods for mutating the list, so you might want to put a Javadoc on the field or getter method that states that the List should not be mutated. Your class should look like this:
public class PopularMovies {
@NotNull
public final List<? extends Result> results;
public PopularMovies(List<? extends Result> results) {
this.results = results;
}
}
Another problem you have is that your Result class is declared as an inner class of your PopularMovies class. That means an instance of it cannot exist apart from an instance of the outer class. You should either mark it as a static class
or move it into its own file.
And when you declare the type List<Result>
in Kotlin, if you don't move your class out of PopularMovies
, then you need to import it specifically at the top of your file, or put the outer class as part of the name, for example List<PopularMovies.Result>
. Otherwise, it thinks you're using the Kotlin standard library Result class of the same name, which is a generic class that expects you to specify a type.
Also, inside your Result class, your fields should either be public final
or they should be private and have getter methods exposed so outside classes can actually get their values. Private fields with public getters is the more recommended approach for future-proofing your class, and it is the direct equivalent of the Kotlin code. But it is a lot more verbose.
You should also change Double
to double
since it is not supposed to hold a null value.
CodePudding user response:
I'm not sure if I understand the question, but something like this?
public class PopularMovies {
@NotNull
public List results;
public PopularMovies(List list) {
results = list;
}
.
.
.