Reputation: 11025
got one question resolved in here
interface IRemoteData<T> {
fun getJsonPOJO() : T?
}
class RemoteData<T>(response: Response<ResponseBody>, pojoClassType: Class<T>) : IRemoteData<T> {
private var mData: T? = null
override fun getJsonPOJO(): T? {
return mData
}
}
now after created a RemoteData instance
val remoteData = RemoteData(response, POJOClazz::class.java)
get another error when passing this remoteData to a function which takes a generic type as well
handler.onRemoteDataReady(remoteData)
//<=== got Type mismatch: inferred type is RemoteData<out Class<T>> but IRemoteData<T>? was expected
the function is defined with in a interface taking d: T?
interface IHandler<T> {
fun onRemoteDataReady(data :T?)
}
the handler instance:
class ResponseHandler(val action: Action) : BaseHandler(action), IHandler<IRemoteData> {
init {
mAction?.preAction(this)
}
override fun onRemoteDataReady(data: IRemoteData?) {
val responseRecieved = data?.getJsonPOJO()
.......
}
}
Upvotes: 0
Views: 46
Reputation: 5287
As stated in the comments, the interface IRemoteData
needs a generic type.
Here's the working example:
interface IHandler<T> {
fun onRemoteDataReady(data :T?)
}
class ResponseHandler<T>(val action: Action) : BaseHandler(action), IHandler<IRemoteData<T>> {
override fun onRemoteDataReady(data: IRemoteData<T>?) {
val responseReceived = data?.getJsonPOJO()
}
}
And it's usage:
val remoteData = RemoteData(response, POJOClazz::class.java)
val handler = ResponseHandler<POJOClazz::class.java>(action)
handler.onRemoteDataReady(remoteData)
Remember from the previous answer, your interface IRemoteData
needs a type. So when you declare the ResponseHandler
class, you need to specify the type as well. A ResponseHandler<String>
will implement IRemoteData<String>
.
Upvotes: 1