Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use custom listener without new?

I want to pass some signal to activity from my custom view. I use some code from the web, but most of them new a view to override some functions. I already have a view in my activity, so I findViewById it, but don’t know why it’s not work.

This is my view code

public class MapView extends View {

public interface MyCustomObjectListener {
    public void onObjectReady(String title);
    public void onDataLoaded();
}

private MyCustomObjectListener listener;

public void setCustomObjectListener(MyCustomObjectListener listener) {
    this.listener = listener;
}
//... some code

and this is my activity code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

        View mapview = findViewById(R.id.mapview); // <---- error
        mapview.setCustomObjectListener(new MapView.MyCustomObjectListener(){
            @Override
            public void onObjectReady(String title) {
            }
            @Override
            public void onDataLoaded() {
            }
        });

        MapView mpview = new MapView(this,null);
        mpview.setCustomObjectListener(new MapView.MyCustomObjectListener(){
            @Override
            public void onObjectReady(String title) {
            }
            @Override
            public void onDataLoaded() {
            }
        });

the error message

error: cannot find symbol
        mapview.setCustomObjectListener(new MapView.MyCustomObjectListener(){
               ^
  symbol:   method setCustomObjectListener(<anonymous MyCustomObjectListener>)
  location: variable mapview of type View

>Solution :

You are using View object for MapView

View mapview = findViewById(R.id.mapview); 

Change this to :

MapView mapview = findViewById(R.id.mapview);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading