Skip to content Skip to sidebar Skip to footer

How To Let Onclick In Listview's Adapter Call Activity's Function

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 Activity

Solution 1:

Inside onClick() Do something like this:

((ParentClass) context).functionToRun();

Solution 2:

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

publicclassMyAdapterextendsBaseAdapter<Long> {
    publicMyAdapter(Activity activity,
            TreeStateManager<Long> treeStateManager, int numberOfLevels) {
        super(activity, treeStateManager, numberOfLevels);
    }

    @OverridepublicvoidhandleItemClick(final View view, final Object id) {
        ((MyActivity) this.activity).someFunction();
    }
}

Then just declare someFunction in MyActivity to do what you want

protectedvoidsomeFunction(){
//    Do something here
    }

Post a Comment for "How To Let Onclick In Listview's Adapter Call Activity's Function"