允许从 OnClickListener 非最终变量修改对象
我有一个适配器,可以在其中加载我的用户和一个用于发送邀请的按钮/文本文件。在适配器中,我这样做:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
UserAgenda ua = getItem(position);
ViewHolder holder;
if (convertView == null) {
//...
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(ua.getInvited() != null){
if(ua.getInvited().equals("true")){
addTextViewInvited(convertView);//add a textview as the user is invited
}
else if(ua.getInvited().equals("false")){
addButton2Invite(convertView,ua);//add the button to invite
}
}
return convertView;
}
在 addButton2Invite 的方法上,我将邀请发送为:
private void addButton2Invite(final View convertView, UserAgenda ua) {
bt2Invited.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
final Button bt2Invited = (Button) convertView.findViewById(R.id.bt2invite);
final ProgressBar pbar = (ProgressBar) convertView.findViewById(R.id.progress);
new Thread(){
@Override
public void run() {
if(networkWork())
ua.setInvited("true"); //Error, NOT PERMITED! ua need to be final.
};
}.start();
}
});
}
当网络工作正常时,我想设置 ua.setInvited("true"); 但这不是如果我不将 ua 声明为 final
,则允许。但随后我无法更改其值,下次适配器进入 getView 时将找到 ua.getInvited() = "false"。当我向下/向上滚动时,文本视图和按钮出现在行上。
有什么想法可以更改 onClicklistener
中 UserAgenda ua 对象的值吗?
谢谢!
I have an adapter where I load my users and one button/textfiled for send invitations. In the adapter I do this:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
UserAgenda ua = getItem(position);
ViewHolder holder;
if (convertView == null) {
//...
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(ua.getInvited() != null){
if(ua.getInvited().equals("true")){
addTextViewInvited(convertView);//add a textview as the user is invited
}
else if(ua.getInvited().equals("false")){
addButton2Invite(convertView,ua);//add the button to invite
}
}
return convertView;
}
And on the method of addButton2Invite I send the invitation as:
private void addButton2Invite(final View convertView, UserAgenda ua) {
bt2Invited.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
final Button bt2Invited = (Button) convertView.findViewById(R.id.bt2invite);
final ProgressBar pbar = (ProgressBar) convertView.findViewById(R.id.progress);
new Thread(){
@Override
public void run() {
if(networkWork())
ua.setInvited("true"); //Error, NOT PERMITED! ua need to be final.
};
}.start();
}
});
}
When the networkWork is OK, I want to set the ua.setInvited("true");
but that is not permited if I dont declare ua as final
. But then I cant change its value and next time the adapter enters on getView will find the ua.getInvited() = "false"
. When I scroll down/up the textview and the button appear on the row.
Any ideas to change the value of the object UserAgenda ua within the onClicklistener
?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
声明 ua 最终,因为当处于 ua.setInvited(true); 时您正在更改 ua 引用的对象中的值而不是 ua 的引用,因此通过设置 ua Final 不会影响您的 setInvited(true)
否则您必须通过在活动类中实现它来使用点击侦听器,例如
使用
和实现以下方法
declare the ua final because when at ua.setInvited(true); you are changing a value in object refrenced by ua not the refrence of ua so by setting ua final will not affect your setInvited(true)
otherwise you have to use click listener by implementing it in your activity class like
and using
and implementing the method of
在Java中,关键字final,当应用于引用类型的变量时,意味着它的引用不能改变,而不是底层对象不能被修改(后者是称为不变性)。
例如,这意味着在您的方法中,如果
ua
被声明为final,您可以这样做:但不能
In Java, the keyword final, when applied to a variable of a reference type, means that its reference cannot change, not that the underlying object cannot be modified (the later is called immutability).
E.g., it means that in your method, if
ua
was declared as final, you could do:but not