While I am well aware that it isn’t possible for me to refer to a non-final variable inside my onClick method, I am interested in knowing how I solve the problem I am currently faced with.
I have an ArrayAdapter. If a user presses the checkbox an AsyncTask and the user either subscribes or unsubscribes himself to a queue, depending on whether the checkbox is checked or not.
I want this status to be reflected in each item in the text of my txtStatus TextView. How do I set the text of a TextView depending on the status of a Checkbox?
Here is part of the code from my getView method :
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
final CheckBox queueCBox;
userControl = new UserController(context);
final String queueName = data[position].label;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
final QueueHolder holder = new QueueHolder();
holder.queuecbox = (CheckBox)row.findViewById(R.id.queues_item_checkbox);
queueCBox = (CheckBox)row.findViewById(R.id.queues_item_checkbox);
holder.txtLabel = (TextView)row.findViewById(R.id.queues_item_label);
holder.txtStatus = (TextView)row.findViewById(R.id.queues_item_status);
holder.queuecbox.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
final String[] param = new String[1];
param[0] = queueName;
if(queueCBox.isChecked())
{
holder.txtStatus.setText("Tilmeldt");
Log.d("ClickTest","SWITCH ON");
new JoinQueueTask().execute(param);
}
else
{
holder.txtStatus.setText("Frameldt");
Log.d("ClickTest","SWITCH OFF");
new LeaveQueueTask().execute(param);
}
}
});
row.setTag(holder);
}
else
{
final QueueHolder holder = (QueueHolder)row.getTag();
}
QueueItem queue = data[position];
String name = queue.label.replace("-", " ");
holder.txtLabel.setText(name); // holder cannot be resolved
holder.queuecbox.setChecked(queue.value); // holder cannot be resolved
return row;
}
Keep your holder the way it is. Just for setting the onClickListener, try doing this,