Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling method of another class using contexts

I have a custom titlebar with an ImageButton, which produces a dialogbox, and I want to be able to show location(place itemizedOverlay) on a map(in another class) when list item is selected from dialog box, and titlebar and map are in the same context. I read somewhere that I could call a method of another class using contexts. How can I do so?

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();
          }
        }
    });
like image 383
consprice Avatar asked Jul 24 '12 16:07

consprice


1 Answers

It looks like I can do this like so:

UserHome userhome = (UserHome)context;
userhome.DisplayLocation();

where DisplayLocation() in the UserHome Activity. Simple.

like image 153
consprice Avatar answered Oct 28 '22 02:10

consprice