I'm trying to have a CountDownTimer inside each row in a ListView. The only place I can have my onClick methods for the button in each row, is in the CustomListAdapter. But I can't refer to the TextView where the time left should be displayed. I get this error:
Cannot refer to a non-final variable holder inside an inner class defined in a different method
I have tried setting the holder to final, but then i get this error:
The final local variable holder cannot be assigned. It must be blank and not using a compound assignment
This is my code:
public class CustomTimerRowAdapter extends ArrayAdapter < TimerRow > {
Context context;
int height;
public CustomTimerRowAdapter(Context context, int resourceId,
List < TimerRow > items) {
super(context, resourceId, items);
this.context = context;
// Height of screen from not Activity subclass
height = context.getResources().getDisplayMetrics().heightPixels;
}
/* private view holder class */
private class ViewHolder {
TextView txtTimer;
TextView txtName;
Button bStartStop;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
final TimerRow rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.timer_row, null);
holder = new ViewHolder();
// make layout params
holder.txtTimer = (TextView) convertView.findViewById(R.id.tvTimes);
holder.txtTimer.getLayoutParams().height = height / 10;
holder.txtName = (TextView) convertView.findViewById(R.id.tvName);
holder.txtName.getLayoutParams().height = height / 10;
holder.bStartStop = (Button) convertView
.findViewById(R.id.bStartStopTimer);
holder.bStartStop.getLayoutParams().height = height / 10;
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.txtName.setText(rowItem.getName());
holder.bStartStop.setOnClickListener(new OnClickListener() {
final ViewHolder temp = new ViewHolder();@
Override
public void onClick(View v) {
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
// THIS IS WHERE I CANNOT REFER TO THE NON-FINAL VARIABLE
holder.txtTimer.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
holder.txtTimer.setText("done!");
}
}.start();
}
});
return convertView;
}
}
Any suggestions on how to fix this?
OnClickListenerin your outer class and get rid of the inner class. Then you can set it like:holder.bStartStop.setOnClickListner(this)- Naveed