我有一个带有 ImageButton 的自定义标题栏,它会生成一个对话框,我希望能够在从对话框中选择列表项时在 map 上(在另一个类中)显示位置(放置 itemizedOverlay),以及标题栏和 map 处于相同的上下文中。我在某处读到我可以使用上下文调用另一个类的方法。我该怎么做?
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 Activity 中的 DisplayLocation()。简单。
关于android - 使用上下文调用另一个类的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11635086/