0

I have this code in kotlin. I am not very familiar with kotlin and hence facing difficulty converting this to java.

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        val recyclerView = view.findViewById<RecyclerView>(R.id.recycler_view)
        val adapter = StackCardAdapter(activity!!.applicationContext)
        adapter.onItemClickListener = { cardView, cardViewModel ->
            val fromPosition = thelist.indexOf(cardViewModel)
            val toPos = 0 
      }

The StackCardAdapter is as follows:

class StackCardAdapter(context: Context) : ListAdapter<CardViewModel, CardViewHolder>(diffUtil) {
    companion object {
           // irrelevant code here 
    }

    var onItemClickListener: ((cardView: CardView, cardViewModel: CardViewModel) -> Unit)? = null


EDIT: Not a listener to the recyclerView. Need to implement lambda functions to create onItemClickListener for ListAdapter

I need to implement the onItemClickListener in OnViewCreated of my fragment. Thank you

vaishak bg
  • 43
  • 1
  • 5

2 Answers2

0

You can convert you kotlin code into java by compiling the .kt file which will generate a .class file.Now you can decompile it using any decompiler if you are using intellij ide that it would give you a decompiled file automatically

Sambit Nag
  • 28
  • 5
  • All it says is this ``` public open fun onViewCreated(view: android.view.View, savedInstanceState: android.os.Bundle?): kotlin.Unit { /* compiled code */ } } ``` // Implementation of methods is not available – vaishak bg Jun 04 '20 at 13:14
  • Followed this method to convert the code. Definitely worked as a temporary solution. Thank you – vaishak bg Jun 08 '20 at 09:48
0

You can use java lambda functions as well, they are similar to kotlin ones:

adapter.setOnItemClickListener((cardView, cardViewModel) -> {
    var fromPosition = thelist.indexOf(cardViewModel)
    var toPos = 0 
}

In the Adapter class, you cannot define a function, you have to create an interface which holds a lambda signature.

public interface CardViewClickListener {
    public void onClick(CardView cardView, CardViewModel cardViewModel);
}

class StackCardAdapter(...) {
    CardViewClickListener onItemClickListener;
}
Animesh Sahu
  • 5,281
  • 2
  • 7
  • 33