gpt4 book ai didi

java - 不能引用内部类中的非最终变量

转载 作者:行者123 更新时间:2023-12-01 07:57:08 24 4
gpt4 key购买 nike

我试图在 ListView 的每一行中添加一个 CountDownTimer。我唯一可以为每行中的按钮使用 onClick 方法的地方是 CustomListAdapter 中。但我无法引用应该显示剩余时间的 TextView 。我收到此错误:

Cannot refer to a non-final variable holder inside an inner class defined in a different method

我尝试将支架设置为最终状态,但随后出现此错误:

The final local variable holder cannot be assigned. It must be blank and not using a compound assignment

这是我的代码:

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

关于如何解决这个问题有什么建议吗?

最佳答案

这应该有效:

最终 ViewHolder 持有者;

即最初不要将其分配为 null。

关于java - 不能引用内部类中的非最终变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28613953/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com