android – 如何在活动中调用非活动类的方法

前端之家收集整理的这篇文章主要介绍了android – 如何在活动中调用非活动类的方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个Activity和非Activity类.如何从非Activity类调用Activity类中的方法
public class MainActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);

        DataClass dc = new DataClass();
        dc.show();                  
    }

    public void call(ArrayList<String> arr) {
       // Some code...
    }
}
public class DataClass {

    public void show(ArrayList<String> array) {
        // Here I want to send this ArrayList values into the call
        // method in activity class.

       MainActivity act = new MainActivity();
       act.call(array);                  
    }
}

@R_403_323@

只需在DateClass中创建一个回调接口.
public DateClass {
    public interface IDateCallback {
        void call(ArrayList<String> arr);
    }  

    private IDateCallback callerActivity;

    public DateClass(Activity activity) {  
        callerActivity = (IDateCallback)activity;  
    }
...  
}  

public void show(ArrayList<String> array) {            
    callerActivity.Call(array);  
    ...  
}

//And implements it inside your activity.

public class MainActivity extends Activity 
        implements IDateCallback {  

    public void call(ArrayList<String> arr) {

    }  
}

猜你在找的Android相关文章