允许从 OnClickListener 非最终变量修改对象

发布于 2024-12-10 22:51:47 字数 1811 浏览 0 评论 0原文

我有一个适配器,可以在其中加载我的用户和一个用于发送邀请的按钮/文本文件。在适配器中,我这样做:

    @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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往昔成烟 2024-12-17 22:51:47

声明 ua 最终,因为当处于 ua.setInvited(true); 时您正在更改 ua 引用的对象中的值而不是 ua 的引用,因此通过设置 ua Final 不会影响您的 setInvited(true)

否则您必须通过在活动类中实现它来使用点击侦听器,例如

public MyActivity extends Activity implements OnClickListener

使用

myView.setOnClickListener(this);

和实现以下方法

public void onClick(View v)
 {
    switch (v.getId()) 
       {
         case R.id.myView:
          //functionality for on click
         break;
        }
  }

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

public MyActivity extends Activity implements OnClickListener

and using

myView.setOnClickListener(this);

and implementing the method of

public void onClick(View v)
 {
    switch (v.getId()) 
       {
         case R.id.myView:
          //functionality for on click
         break;
        }
  }
稀香 2024-12-17 22:51:47

在Java中,关键字final,当应用于引用类型的变量时,意味着它的引用不能改变,而不是底层对象不能被修改(后者是称为不变性)。

例如,这意味着在您的方法中,如果 ua 被声明为final,您可以这样做:

ua.some_pub_non_final_field = "some_value";
ua.somePublicMethod();

但不能

ua = new UA();

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:

ua.some_pub_non_final_field = "some_value";
ua.somePublicMethod();

but not

ua = new UA();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文