从其创建的对象中访问调用活动的方法

时间:2011-12-08 10:35:17

标签: android methods

MyI不明白我为什么会收到编译错误:

   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中:

 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实例。从这个实例中我想要调用调用者的方法。

为什么不能这样做?

非常感谢!

4 个答案:

答案 0 :(得分:5)

试试这个:

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

上下文是Activity,它没有doSth(),但是Main_screen有,所以你应该转换为这个类

答案 1 :(得分:1)

实际上你正在制作Activity context个对象并传递Activity的孩子(即Main_Screen),其被称为向上演员(Implicit Casting)。

因此Activity(作为父级)没有doSth()的方法。因此,您需要向下投射(显式投射)以使其成为Main_Screen

两种方法。

制作一个Main_Screen context而不是Activity context

的对象

Main_Screen方法中将其转换为tryIt(),以便像这样使用Main_Screen方法:

if(context.isInstance(Main_Screen.class))
{
    ((Main_Screen)context).doSth()
}

您还可以使用try catch来最小化ClassCastException

的可能性

答案 2 :(得分:0)

您可以使用以下代码。显然,类context不包含对象doSth()doSth()中声明了class Main_screen

 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() {
           Main_screen.doSth();
        }
    }

答案 3 :(得分:0)

我是怎么做的

StaticCommonDataClass - &gt;在这里维护静态数据我会在其中保留Activity 1的实例。

ActivityOneClass - &gt;包含我必须在ActivityTwo中实际访问的方法。

ActivityTwoClass =&gt;将访问ActivityOne方法。

我讨厌的是将两个参数从一个函数传递给另一个函数或将一个类传递给其他类, 当必须一次又一次地为类似的值做完时。

这里我将在静态变量中存储对ActivityOneClass 的引用。

public class CommonStaticData {

    private static Activity _activity;
    private static Context _context;

    public static void setactivity(Activity activity) {
        _activity = activity;
    }

    public static Activity getactivity() {
        return _activity;
    }

    public static void setcontext(Context context) {
        _context = context;
    }

    public static Context getcontext() {
        return _context;
    }



}


public class ActivityOneClass extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity1);

        CommonStaticData.setactivity(ActivityOneClass.this); //will keep the instance alive for this activity
    }

    Public void activityOneMethod()
    {
        //Set of statements 
    }
}



public class ActivityTwoClass extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity2);

        ((ActivityOneClass) CommonStaticData.getactivity()).activityOneMethod();  

        //we need to typecast the instance stored in CommonStaticData.getactivity() to "ActivityOneClass" thats is the 
        //activity containing the method so as to access the method otherwise it will not come in the intellisense window and will generate Compiler Error
    }
}