Accessing variables from onclicklistener

后端 未结 2 1962
猫巷女王i
猫巷女王i 2021-01-22 11:59

So I have an onItemLongClickListener for a list view that gets a parameter \'int position\' passed in. Inside, I have an alertDialogBuilder which has two buttons. I have another

相关标签:
2条回答
  • 2021-01-22 12:19

    In Java, you can access outer variables from an inner class declaration only if they are first declared as final. So you could add:

    final int selectedPosition = position;
    

    to the top of your method and use that value in your extra, i.e.

    intent.putExtra("position", selectedPosition);
    
    0 讨论(0)
  • 2021-01-22 12:29

    If you change position to final, then it should let you access it from within the onClick method.

    0 讨论(0)
提交回复
热议问题