不能在内部类中引用非final变量

时间:2015-02-19 18:19:22

标签: java android

我正试图在CountDownTimer的每一行内设ListView。我可以在onClick中使用CustomListAdapter方法获取每行按钮的唯一位置。但我不能参考应该显示剩余时间的TextView。我收到这个错误:

  

不能引用内部类中的非final变量holder   用不同的方法定义

我已尝试将持有者设置为final,但后来我收到此错误:

  

无法分配最终的局部变量持有者。它必须是空白的   而不使用复合作业

这是我的代码:

    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;
      }
    }

有关如何解决此问题的任何建议吗?

3 个答案:

答案 0 :(得分:4)

这应该有效:

final ViewHolder holder;

即最初不要将其指定为null。

答案 1 :(得分:2)

而不是让holder最终使用v.getParent()来访问Button上的TextView,而不是点击:

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...
        }
    });

使用txtTimeronTick中的onFinish更新TextView文字。

答案 2 :(得分:0)

ViewHolder holder声明为类变量。得到它了?没有!看下面。

你的getView将如下所示

    private ViewHolder holder;

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