I'm new using Kotlin and I have to develop an app for Android Auto. I just did write a code for creating the first Hello World screen but now I wanna add a button for navigate to another screen. The problem is that I did this in Java like the code below but I don't know how to write this arrow function (setOnClickListener(() -> getScreenManager().push(new SecondScreen(getCarContext())))
) in Kontlin
This is the code in Java:
public Template onGetTemplate() {
//Row row = new Row.Builder().setTitle("Hello world").addText("Example").build();
MessageTemplate template = new MessageTemplate.Builder("Hello world!").setTitle("Hello")
.addAction(
new Action.Builder()
.setTitle("Next screen")
.setOnClickListener(
() -> getScreenManager().push(new SecondScreen(getCarContext())))
.build())
.build();
return template;
}
I have tried with this: 1)
MessageTemplate.Builder("Hello world!").setTitle("Hello")
.addAction(
Action.Builder()
.setTitle("Next screen")
.setOnClickListener(
() -> getScreenManager().push(SecondScreen(getCarContext()))).build()).build()
And this: 2)
MessageTemplate.Builder("Hello world!").setTitle("Hello")
.addAction(
Action.Builder()
.setTitle("Next screen")
.setOnClickListener(
{() -> getScreenManager().push(SecondScreen(getCarContext()))}).build()).build()
But I only received these results:
Expecting an expression
Expecting a name, Type mismatch: inferred type is (Any?) -> Unit but () -> Unit was expected, Expected no parameters
CodePudding user response:
Did you have tried this?
val template: MessageTemplate = Builder("Hello world!").setTitle("Hello")
.addAction(
Builder()
.setTitle("Next screen")
.setOnClickListener { getScreenManager().push(SecondScreen(getCarContext())) }
.build()
)
.build()