访问它创建了一个对象中的调用活动的方法 [英] accessing a calling activity's method from within an object it created

查看:146
本文介绍了访问它创建了一个对象中的调用活动的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MYI不明白为什么我得到一个编译错误此:

MyI don't understand why I get a compile error for this:

   public class Main_screen extends ListActivity {

       List<Object> completeList;
       private My_ArrayAdapter adapter;


    public void onCreate(Bundle icicle) {
       super.onCreate(icicle);
       completeList = getCompleteList();
       adapter = new My_ArrayAdapter(this, completeList);
       setListAdapter(adapter);
       }

    public void doSth() {
                ...
        }
    }

和在My_ArryAdapter:

and in My_ArryAdapter:

 public class My_ArrayAdapter extends ArrayAdapter<Object> {

        private final List<Object> list;
        private final Activity context;


        public My_ArrayAdapter(Activity context, List<Object> list) {
           this.context = context;
           this.list = list;

        }

        public void tryIt() {
           context.doSth();   // <-------- THIS DOES NOT WORK, this method can not be called
        }
    }

请解释一下,是有什么我根本没有理解。我只是路过的背景下进入一个ArrayAdapter实例创建。而从这个实例中,我想acccess调用者的方法。

Please explain, is there something fundamental I have not understood. I am just passing the context into the ArrayAdapter instance I create. And from within this instance I would like to acccess the caller's method.

为什么shoudl这是不可能的?

Why shoudl this not be possible?

非常感谢!

推荐答案

试试这个:

public void tryIt() {
           ((Main_screen)context).doSth();  
        }

上下文是活动现在也没有doSth(),但Main_screen了,所以你应该转换为该类

context is Activity and it hasn't doSth(), but Main_screen has, so you should cast to this class

这篇关于访问它创建了一个对象中的调用活动的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆