How to let OnClick in ListView's Adapter call Activity's function

纵然是瞬间 提交于 2019-12-03 05:56:39

Inside onClick() Do something like this:

((ParentClass) context).functionToRun();

Just for clarity to expand on provided answers In a BaseAdapter you can get the parent class by calling this.getActivity();If you then typecast this to the actual activity class you can then call a function as per @AdilSoomro answer below so you actually end up with something like this

public class MyAdapter extends BaseAdapter<Long> {
    public MyAdapter(Activity activity,
            TreeStateManager<Long> treeStateManager, int numberOfLevels) {
        super(activity, treeStateManager, numberOfLevels);
    }

    @Override
    public void handleItemClick(final View view, final Object id) {
        ((MyActivity) this.activity).someFunction();
    }
}

Then just declare someFunction in MyActivity to do what you want

    protected void someFunction(){
//    Do something here
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!