我有一个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); } }
解决方法
只需在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) { } }