简体   繁体   English

如何从AlertDialog的onClickListener中访问Activity的实例变量?

[英]How can I access my Activity's instance variables from within an AlertDialog's onClickListener?

Here's a very simplified version of my Activity : 这是我的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;

        } 

    }

}

When the menu button is pressed, my applications pops up an AlertDialog . 当按下菜单按钮时,我的应用程序将弹出AlertDialog When creating the AlertDialog and in-line onClickListener is attached to the each of the items in the dialog. 当创建AlertDialog和在线onClickListener被附接到每个对话框中的项目。 I need to access the objAdapater variable that is defined in my Search activity. 我需要访问在Search活动中定义的objAdapater变量。 I don't have access to the search instance within my onClickListener so I can't access it. 我无权访问onClickListener的搜索实例,因此无法访问它。 I have a little bit of a soup in my code with the passing of the Activity instance everywhere. 在随处都传递Activity实例的情况下,我的代码有些麻烦。 Maybe I'm doing something wrong. 也许我做错了。

How would I get access to the Activity ( Search instance) from within my onClickListener so I can access it's methods and variables. 我如何从onClickListener内访问ActivitySearch实例),以便可以访问它的方法和变量。

Thank you. 谢谢。

Using Search.this.objAdapter to access objAdapter from the listener should work. 使用Search.this.objAdapter从侦听器访问objAdapter应该可以工作。

Search.this refers to the current instance of Search and allow you to access its fields and methods. Search.this指向Search的当前实例,并允许您访问其字段和方法。

Make your activity implement OnClickListener: 使您的活动实现OnClickListener:

public class Search  extends Activity implements DialogInterface.OnClickListener { ...

Add the onclick method to your activity: 将onclick方法添加到您的活动中:

public void onClick(DialogInterface dialog, int item) {
     //I need to access it from here.
}

Then pass your activity as the listener: 然后将您的活动作为侦听器传递:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(itmMenuitem.getTitle());

builder.setSingleChoiceItems(lstChoices),0, this);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从膨胀活动访问 AlertDialog 中的视图 - Access a view within an AlertDialog from the inflating activity 如何从Activity为Fragment的开关设置OnClickListener? - How to set OnClickListener from Activity for Fragment's switch? setText()在另一个Activity的onClickListener上不起作用 - setText() not working from another Activity's onClickListener 如何将Recyclerview的OnClickListener放入Activity - How to put Recyclerview's OnClickListener in Activity 如何从我在该类中创建的类中访问该类的变量? - How do I access a class's variables from a class that I created within that class? 我可以从侦听器内部引用 OnClickListener 的按钮吗? (安卓) - Can I reference an OnClickListener's button from inside the listener? (android) 如何取消alertDialog中的按钮? - How can I unsquash the buttons within my alertDialog? Unity Android插件库中如何访问AlertDialog的消息和标题? (我在 AlertDialog 中使用自定义字体) - How to access AlertDialog's Message and Title in Unity Android Plugin Library? (I am using custom Font in AlertDialog) 我怎么或为什么不能从服务访问活动的属性? - How or why can't I access an Activity's attribute from a Service? 如何在NavigationDrawerItemSelected中访问ViewPager的实例? - How I can access ViewPager's Instance in NavigationDrawerItemSelected?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM