[英]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.我使用了 web 中的一些代码,但它们中的大多数都是新的视图来覆盖某些功能。 I already have a view in my activity, so I findViewById it, but don't know why it's not work.我的活动中已经有一个视图,所以我 findViewById 它,但不知道为什么它不起作用。
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这是我的活动代码
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
You are using View object for MapView您正在为 MapView 使用 View object
View mapview = findViewById(R.id.mapview);
Change this to:将其更改为:
MapView mapview = findViewById(R.id.mapview);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.