使用上下文调用另一个类的方法 [英] Calling method of another class using contexts
本文介绍了使用上下文调用另一个类的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有ImageButton的自定义标题栏,该标题栏生成一个对话框,并且当从对话框中选择列表项时,我希望能够在地图上(在另一个类中)显示位置(将itemizedOverlay放置),标题栏和地图是在相同的上下文中.我在某处读到可以使用上下文调用另一个类的方法的地方.我该怎么办?
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();
}
}
});
推荐答案
看来我可以这样做:
UserHome userhome = (UserHome)context;
userhome.DisplayLocation();
UserHome活动中的DisplayLocation().很简单.
where DisplayLocation() in the UserHome Activity. Simple.
这篇关于使用上下文调用另一个类的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文