Why The Compiler Ask Me Declare A Variable "final" When I Use The Method "onclicklistener" On Adapter Class?
Solution 1:
Because of the way the anonymous inner classes work, it would need to be final. Considering you seem to want to assign this same functionality to each and every row of your list, I believe what you should do is create a class that implements the OnClickListener interface, and holds a reference to the checkBox of which's state the functionality depends on. However, I'm sort of concerned about how many items you have, because if you had many items, this would cause a major memory problem.
So what I thought of just now is, try the following:
holder.checkBox1.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
// TODO Auto-generated method stubCheckBoxcheckBox= (CheckBox) v;
if(checkBox.isChecked()){ // I CAN USE THIS
}
}
});
Because the anonymous class's onClick function is called with the parameter of the View that calls back upon it, therefore the View parameter in this case can only be the CheckBox. I think this is what you need.
Solution 2:
See this answer for a detailed description as to why variables referenced in an anonymous inner class must be final. However, in your case there's a simpler solution:
holder.checkBox1.setOnClickListener(new View.OnClickListener() {
@Override publicvoid onClick(View checkBox) {
if ((Checkable) checkBox).isChecked()) {
// Do Stuff
}
}
}
Since the View
parameter of OnClickListener
is the actual View that was clicked, and you're setting this listener on the CheckBox
, you can simply cast the View
parameter to Checkable
and reference it from there.
EDIT: Also, you might actually be looking for CheckBox.OnCheckedChangeListener
rather than View.OnClickListener
.
Solution 3:
If you put final
in your holder
object then it will place in the heap.
To reduce some memory going to the heap just use only the boolean from the holder as final instead the whole object.
example:
holder.nombre_material.setText(entry.getNombreMaterial());
finalbooleanisChecked= holder.checkBox1.isChecked();
holder.checkBox1.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
// TODO Auto-generated method stubif(isChecked ){
}
}
});
Solution 4:
Short answer - just copy ViewHolder
into final
variable and use it inside View.OnClickListener
. Like this:
finalViewHolderholderRef= holder;
holder.checkBox1.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
// TODO Auto-generated method stubif(holderRef.checkBox1.isChecked()){
}
}
});
Longer answer - you need to make it final basically due to the way Java manages closures.
You are actually creating an instance of anonymous inner subclass of OnClickListener
here. Any variables which are used within that class have their values copied in via the autogenerated constructor. So here is another solution you could have - you can explicitly create OnClickListener
subclass and pass ViewHolder
reference to it.
Post a Comment for "Why The Compiler Ask Me Declare A Variable "final" When I Use The Method "onclicklistener" On Adapter Class?"