Snusifer
Snusifer

Reputation: 553

How to instantiate interface in Kotlin?

setOnPlaceSelectedListener takes an interface as argument. In Java, one could make an instance of that interface by overriding all of the methods on the get-go. How to do this in Kotlin?

autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
    @Override
    public void onPlaceSelected(Place place) {
        txtView.setText(place.getName()+","+place.getId());
        Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());
    }

    @Override
    public void onError(Status status) {
        // TODO: Handle the error.
        Log.i(TAG, "An error occurred: " + status);
    }
});

Upvotes: 1

Views: 650

Answers (1)

Eric Martori
Eric Martori

Reputation: 2975

As you can find the oficial documentation (https://kotlinlang.org/docs/reference/object-declarations.html) you can create an object declaration:

window.addMouseListener(object : MouseAdapter() {
    override fun mouseClicked(e: MouseEvent) { ... }

    override fun mouseEntered(e: MouseEvent) { ... }
})

Upvotes: 3

Related Questions