嵌套对象访问属性家长 [英] Nested object access parents attribute
问题描述
我在Java的还挺新,我有一个简单的问题。在下面的code:
I'm kinda new at Java and I have a simple question. In the code below:
conversation.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
if (adapter.getItem(position).image && adapter.getItem(position).left) {
String imgName = ((Message)arg1.findViewById(R.id.imageUploaded).getTag()).message;
try {
AlertDialog.Builder confirmQuit = new AlertDialog.Builder(Chat.this);
confirmQuit.setMessage("Download image?");
// Buttons on dialog
confirmQuit.setPositiveButton("Download", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
confirmQuit.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
}
});
confirmQuit.create();
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
}
这是一个Android code,我想知道我怎么可以访问的变量位置
和 imgName
里面的 DialogInterface.OnClickListener
?
That's an android code and I would like to know how can I access the variables position
and imgName
inside the DialogInterface.OnClickListener
?
通常在这些情况下,我在我的活动,所以我可以在任何地方访问它创建一个全球性私有变量作为一个属性,但它看起来不正确。有人可以帮忙吗?
Usually in these cases I create a "global" private variable as an attribute on my activity so I can access it anywhere, but it doesn't seem right. Can someone help?
先谢谢了。
推荐答案
只要修改
String imgName = ...
到
final String imgName = ...
从Java教程匿名类的:
访问封闭范围内的局部变量
像本地类,匿名类可以捕捉变量;他们有
同样进入封闭范围的局部变量:
Like local classes, anonymous classes can capture variables; they have the same access to local variables of the enclosing scope:
- 匿名类可以访问它包围类的成员。
- 匿名类的不能访问它的封闭范围内的局部变量未声明为final 的或有效地决赛。
- An anonymous class has access to the members of its enclosing class.
- An anonymous class cannot access local variables in its enclosing scope that are not declared as final or effectively final.
这篇关于嵌套对象访问属性家长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!