2012-07-24 21 views
4

我有一個帶ImageButton的自定義標題欄,它會生成一個對話框,並且我希望在選擇列表項時能夠在地圖上(在另一個類中)顯示位置(放置itemizedOverlay)來自對話框,並且標題欄和地圖處於相同的上下文中。我讀了一些地方,我可以使用上下文來調用另一個類的方法。我該怎麼做?使用上下文調用另一個類的方法

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(); 
      } 
     } 
    }); 

回答

14

它看起來像我能做到這一點,像這樣:

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

其中DisplayLocation()在USERHOME活動。簡單。