0
votes

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?

3
You could implement the OnClickListener in your outer class and get rid of the inner class. Then you can set it like: holder.bStartStop.setOnClickListner(this) - Naveed

3 Answers

4
votes

This should work:

final ViewHolder holder;

ie don't assign it to null initially.

2
votes

Instead of making holder final use v.getParent() for accessing TextView on Button click like:

holder.bStartStop.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
             View parent = (View)v.getParent();
             final TextView txtTimer = parent.findViewById(R.id.tvTimes);
             //... your code here...
        }
    });

Use txtTimer in onTick and onFinish for updating TextView text.

0
votes

Declare ViewHolder holder as class variable. Got it? No! look below.

your getView will look like below

    private ViewHolder holder;

    public View getView(int position, View convertView, ViewGroup parent) {
         holder = null;