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

后端 未结 2 1512
小蘑菇
小蘑菇 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<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
        }
    
    0 讨论(0)
  • 2021-02-07 23:16

    Inside onClick() Do something like this:

    ((ParentClass) context).functionToRun();
    
    0 讨论(0)
提交回复
热议问题