2

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!

Vaandu
  • 4,857
  • 12
  • 49
  • 75
Dayerman
  • 3,973
  • 6
  • 38
  • 54
  • Did you tried it to make final and changed value and then checked its value..? – Adil Soomro Oct 21 '11 at 13:04
  • 1
    `final` makes it so that you cannot reassign a variable. I do not see where you are assigning `ua` in this code. If a variable is `final`, you can still call mutation methods on it. Perhaps you do reassign `ua` at some point, I just don't see it in this code, so I do not see why you can't mark it as `final`. – nicholas.hauschild Oct 21 '11 at 13:07
  • +1 agree with @nicholas.hauschild – Adil Soomro Oct 21 '11 at 13:10

2 Answers2

5

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;
        }
  }
Umar Qureshi
  • 5,985
  • 2
  • 30
  • 40
3

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();
JRL
  • 76,767
  • 18
  • 98
  • 146