Q:

Modify an object from OnClickListener non-final variable permited

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!

A:

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;
        }
  }

A:

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();

