【发布时间】:2012-08-28 10:39:45
【问题描述】:
这是我的Activity 的一个非常简化的版本:
public class Search extends Activity {
//I need to access this.
public SearchResultsAdapter objAdapter;
public boolean onOptionsItemSelected(MenuItem itmMenuitem) {
if (itmMenuitem.getItemId() == R.id.group) {
final CharSequence[] items = {"Red", "Green", "Blue"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(itmMenuitem.getTitle());
builder.setSingleChoiceItems(lstChoices),
0, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
//I need to access it from here.
}
});
AlertDialog alert = builder.create();
alert.show();
return true;
}
}
}
按下菜单按钮时,我的应用程序会弹出一个AlertDialog。在创建AlertDialog 和内联onClickListener 时,将附加到对话框中的每个项目。我需要访问在我的Search 活动中定义的objAdapater 变量。我无法访问我的onClickListener 中的搜索实例,所以我无法访问它。随着 Activity 实例随处可见,我的代码中有一点点汤。也许我做错了什么。
如何从我的onClickListener 中访问Activity(Search 实例),以便访问它的方法和变量。
谢谢。
【问题讨论】:
标签: java android scope instances