从Android中的扩展视图类调用活动方法

OVE*_*ONE 4 android view hierarchy android-activity

这是我在设计应用程序结构时没有预料到的问题.

基本上我有3个班级.

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.
Run Code Online (Sandbox Code Playgroud)

该活动包含多个ProfileView,每个ProfileView包含一个profileData.

在我被困的地方.我的个人资料视图分配了一个点击方法,需要在活动中调用填充方法.不幸的是````

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

有没有办法从profileView类调用activity方法?

如果没有,那么错误就在我的设计中,任何人都可以指出我为绑定数据,视图和活动提供更好的解决方案吗?

dre*_*ale 9

创建视图时,始终需要上下文,并始终将活动传递给它.所以尝试使用下面的代码:

If(m_context instanceof ProfileActivity)
{
    ProfileActivity activity = (ProfileActivity)m_context;
    // Then call the method in the activity.
}
Run Code Online (Sandbox Code Playgroud)

或者编写一个接口,onProfileViewClickListener例如view.onclickListener.然后ProfileActivity实现它并将其设置为ProfileView.