[英]How to call a public method of an Activity from another Android Activity
[英]How to call public methood of myCustomAdapter in Main Activity?
我有MainActivity和适配器。 创建适配器的新实例后,我将创建新领域。 我该如何关闭onDestroy模式? 这是我尝试过的:
MainActivity的onCreate:
public class MainActivity extends AppCompatActivity {
private ExpandableListView myELV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final android.support.v7.app.ActionBar actionBar = this.getSupportActionBar();
myELV = findViewById(R.id.expandable_list_view);
myAdapter adapter = new myAdapter(getApplicationContext());
myELV.setAdapter(adapter);
适配器:
public class myAdapter extends BaseExpandableListAdapter{
private Context context;
private Realm realm;
public myAdapter(Context context){
this.context = context;
}
public void closeRealm(){
realm.deleteAll();
realm.close();
}
MainActivity的onDestroy:
@Override
protected void onDestroy() {
super.onDestroy();
ExpandableListAdapter adapter = myELV.getExpandableListAdapter();
adapter.closeRealm();
}
但是即使公开,也无法访问closeRealm()。 我想知道为什么?
回响@EpicPandaForce的答案, ExpandableListAdapter是接口类型。 您的closeRealm()方法不是该接口中的方法之一。 要解决此问题,您可以做的一件事情就是创建一个扩展ExpandableListAdapter的新接口,并在其中列出所需的新公共方法的签名。 然后,可以将实现您的新接口的任何类替换为其他实现您的新接口的类。
是的,使用@EpicPandaForce的答案,如果将来有人想知道的话,我将在代码中发布更改:
@Override
protected void onDestroy() {
super.onDestroy();
myAdapter adapter = (myAdapter) myELV.getExpandableListAdapter();
adapter.closeRealm();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.