Reputation: 439
I'm trying to implement a clickListener in an Android RecyclerView, with fragments, data binding, ViewModel/LiveData, and my code has been constructed based on practices in the Udacity 'Developing Android Apps with Kotlin' course. However, my project is in Java as all the backend is already written. I don't know enough Kotlin and can not work out how to implement the following in Java!
class SleepNightListener(val clickListener: (sleepId: Long) -> Unit)
fun onClick(night: SleepNight) = clickListener(night.nightId)
}
I have looked online at other people who've implemented this type of structure in Java, but it's always been done in a different way that causes problems with the way my ViewAdapter is structured.
Thanks in advance.
Upvotes: 1
Views: 251
Reputation: 5635
So, the Kotlin
snipped you have posted in your question can be implemented in Java
as:
interface ClickListener {
void onCLick(Long sleepId);
}
class SleepNightListener {
private ClickListener clickListener;
public SleepNightListener(ClickListener clickListener) {
this.clickListener = clickListener;
}
public void onClick(SleepNight night) {
if (clickListener != null) clickListener.onCLick(night.getNightId());
}
}
Upvotes: 2