-2

I am trying to write the code below written in Kotlin, in Java. I am not able to create the DefaultElementsAdapter as I am unable to get the syntax right.

HtmlRecycler.Builder(this@MainActivity)  
    .setSource(networkSource)  
    .setAdapter(DefaultElementsAdapter(this@MainActivity) { element, i, view ->  
    }})
    .setRecyclerView(recyclerView)  
    .setLoadingCallback(object : HtmlRecycler.LoadCallback {  
        override fun onLoadingStart() {  
            progressBar.visibility = View.VISIBLE  
        }  
        override fun onLoaded(document: Document?) {  
            progressBar.visibility = View.GONE
    }  
    })  
    .build()

I am unable to get this right Java Code

    DefaultElementsAdapter adapter = new DefaultElementsAdapter(this, 

 /****How to create the kotlin adapter here****/


    );

            HtmlRecycler.Builder builder = new HtmlRecycler.Builder(this);
            builder.setSource(new StringSource(item.contentDescription))
                    .setRecyclerView(recyclerView)
                    .setAdapter(adapter)
            .build();

The Kotlin class is something like this

public final class DefaultElementsAdapter public constructor(context: android.content.Context, onClick: (m7mdra.com.htmlrecycler.elements.Element, kotlin.Int, android.view.View) -> kotlin.Unit) : m7mdra.com.htmlrecycler.adapter.ElementsAdapter {
    private final val context: android.content.Context /* compiled code */

    private final val onClick: (m7mdra.com.htmlrecycler.elements.Element, kotlin.Int, android.view.View) -> kotlin.Unit /* compiled code */

    public open fun onBindElement(holder: androidx.recyclerview.widget.RecyclerView.ViewHolder, position: kotlin.Int): kotlin.Unit { /* compiled code */ }

    public open fun onCreateElement(parent: android.view.ViewGroup, elementType: m7mdra.com.htmlrecycler.elements.ElementType): androidx.recyclerview.widget.RecyclerView.ViewHolder { /* compiled code */ }
}

I am trying to use the library https://github.com/m7mdra/HtmlRecycler

ichthyocentaurs
  • 2,173
  • 21
  • 35

1 Answers1

4

DefaultElementAdapter constructor expects its second argument to be a function of type (Element, Int, View) -> Unit, which means that it has to accept 3 arguments, Element, Int (int or Integer in Java) and View, and doesn't return anything.

If you have enabled Java 8 support in your Android project, you can provide the needed function using a lambda expression:

DefaultElementsAdapter adapter = new DefaultElementsAdapter(this, (element, i, view) -> {
    // your code
    return Unit.INSTANCE;
});

If you don't have Java 8 support and can't use lambdas, you can always create a Function3 object:

DefaultElementsAdapter adapter = new DefaultElementsAdapter(this, new Function3<Element, Integer, View, Unit>() {
    @Override
    public Unit invoke(Element element, Integer integer, View view) {
        // your code
        return Unit.INSTANCE;
    }
});

Note that unfortunately you have to return Unit.INSTANCE in Java in both cases. The reason behind such need has been already explained here.

jsamol
  • 3,042
  • 2
  • 16
  • 27