Calling activity method from extended view class in android

This is a problem that I did not consider when designing the structure of my application.

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 ProfileView profiles, each containing one profile.

This is where I am stuck. My Profile View has an assigned click method that should call the fill method on the activity. Unfortunately, `` ''

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

      

Is it possible to call the activity method from the profileView class?

If not, it's a mistake in my design and can someone point me to a better solution for data binding, view and action?

+3


source to share


2 answers


When you create a view, you always need a context and always pass an action to it. So try using below code:

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

      



Or write an interface onProfileViewClickListener

like view.onclickListener. then ProfileActivity implements it and sets it to ProfileView.

+7


source


How about if you assign OnClickListener or OnTouchListener in your ProfileActivity class and in that Listener you cannot call the fill method. In the OnTouchListener, you can get the position of the TouchEvent so that you can assign it to a specific profile.



0


source







All Articles