Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling activity method from extended view class in Android

This is a problem I didn't forsee when designing the structure of my applicaton.

Basically i have 3 classes.

ProfileActivity //The main activity for the application.
ProfileView     //Which extends Linear Layout so that I can tinker with my own view. Note that this is just part of the whole activity and not the main layout
ProfileData     //This is just a class for storing data.

The activity contains multiple ProfileViews which each contain one profileData.

Heres where I'm stuck. My profile View has an on click method assigned which needs to call a populate method in the activity. Unfortunately ````

//I'm inside the activity
public void populateProfileDataForm(ProfileData pd)
            {
                //edit some text fields and other widgets from here
            }

Is there any way to call the activity method from the profileView class?

If not, then the error is in my design and can anyone point me towards a better solution for binding data, view and activitys?

like image 922
OVERTONE Avatar asked Jan 17 '23 02:01

OVERTONE


1 Answers

When you create a view, you always need a context, and always pass a activity to it. So try use below code:

If(m_context instanceof ProfileActivity)
{
    ProfileActivity activity = (ProfileActivity)m_context;
    // Then call the method in the activity.
}

Or write an interface onProfileViewClickListener such as view.onclickListener. then the ProfileActivity implements it and set it to the ProfileView.

like image 74
dreamtale Avatar answered Jan 20 '23 16:01

dreamtale