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

后端 未结 2 1559
小蘑菇
小蘑菇 2021-02-07 22:29

I have an Android application with a ListView in it, the ListView will setup fine but now I want a image in the ListView to be clickable. I do this by using 2 classes, the Activ

2条回答
  •  轮回少年
    2021-02-07 23:02

    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 {
        public MyAdapter(Activity activity,
                TreeStateManager 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
        }
    

提交回复
热议问题