Android 使用上下文调用另一个类的方法

Android 使用上下文调用另一个类的方法,android,android-context,Android,Android Context,我有一个带有ImageButton的自定义标题栏,该标题栏会生成一个对话框,我希望在从对话框中选择列表项,并且标题栏和地图位于同一上下文中时,能够在地图(在另一个类中)上显示位置(place ItemizeOverlay)。我在某个地方读到,我可以使用上下文调用另一个类的方法。我怎样才能做到 public class MyTitleBar extends RelativeLayout{ private Context context; public MyTitleBar(Context c

我有一个带有ImageButton的自定义标题栏,该标题栏会生成一个对话框,我希望在从对话框中选择列表项,并且标题栏和地图位于同一上下文中时,能够在地图(在另一个类中)上显示位置(place ItemizeOverlay)。我在某个地方读到,我可以使用上下文调用另一个类的方法。我怎样才能做到

public class MyTitleBar extends RelativeLayout{

private Context context;


public MyTitleBar(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context = context;
}

@Override
protected void onFinishInflate() {

    super.onFinishInflate();

    initViews();
}

// set up all the buttons  & clicks
private void initViews() {

    final ImageButton listImgBtn = (ImageButton) findViewById(R.id.more);
    final CharSequence [] listItems = {"Elderly","Events"};

    listImgBtn.setOnClickListener(new  View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(context instanceof UserHome)
            {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("List");
                builder.setItems(listItems, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int item) {
                    // TODO Auto-generated method stub
                    if(item == 0)
                    {
                        //show location of elderly
                       //DisplayLocation()

                    }
                    else if(item == 1)
                    {
                        //show location of events
                    }
                }
            });
            AlertDialog alert = builder.create();
            alert.show();
          }
        }
    });

看起来我可以这样做:

UserHome userhome = (UserHome)context;
userhome.DisplayLocation();
其中,UserHome活动中的DisplayLocation()。简单