我如何实施视图持有者?
private class ViewHolder {
TextView code, txtv;
CheckBox name;
Button btnnn;
}
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.abb_list, null);
holder = new ViewHolder();
holder.code = (TextView) convertView.findViewById(R.id.code);
holder.name = (CheckBox) convertView
.findViewById(R.id.checkBox1);
holder.btnnn = (Button) convertView.findViewById(R.id.btn);
holder.txtv = (TextView) convertView.findViewById(R.id.txt);
convertView.setTag(holder);
holder.name.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
checkTable country = (checkTable) cb.getTag();
country.setSelected(cb.isChecked());
}
});
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.btnnn.setOnClickListener(new OnClickListener() {
int counter = 0;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
holder.txtv.setText(String.valueOf(counter));
}
});
checkTable checkTable = itemsList.get(position);
holder.code.setText(" (" + checkTable.getitemprice() + ")");
holder.name.setText(checkTable.getitemname());
holder.name.setChecked(checkTable.isSelected());
holder.name.setTag(checkTable);
return convertView;
}
}
当我正在holder.txtv.setText(String.valueOf(counter));
时,它会向我显示错误Cannot refer to the non-final local variable holder defined in an enclosing scope
建议我如何解决此问题。
答案 0 :(得分:2)
您可以使用final
TextView
作为监听器:
final TextView counterTxt = holder.txtv;
holder.btnnn.setOnClickListener(new OnClickListener() {
int counter = 0;
@Override
public void onClick(View v) {
counter++;
counterTxt.setText(String.valueOf(counter));
}
});
答案 1 :(得分:1)
在方法之外声明一些变量,作为私有变量:
private TextView counterTx;
private int counter;
private ViewHolder holder;
编辑:我认为这是一个更好的实现,将这些变量声明为final
,因为之后您无法更改其值。
答案 2 :(得分:0)
我建议您将 findViewById 和 View.OnClickListener 移至ViewHolder。还要在其中保留一个checkTable引用。这将使代码更清晰。
这种风格是从学到的。 RecylerView.ViewHolder旨在取代ListView,并应在未来的项目中使用。
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = LayoutInflater.from(parent.getContext());
convertView = vi.inflate(R.layout.abb_list, parent, false);
holder = new ViewHolder(convertView);
} else {
holder = (ViewHolder) convertView.getTag();
}
checkTable checkTable = itemsList.get(position);
holder.country = checkTable;
holder.code.setText(" (" + checkTable.getitemprice() + ")");
holder.name.setText(checkTable.getitemname());
holder.name.setChecked(checkTable.isSelected());
return convertView;
}
// Try to use static inner class as possible as you can, this will eliminate
// many strange issues.
private static class ViewHolder implments View.OnClickListener {
checkTable country;
TextView code, txtv;
CheckBox name;
Button btnnn;
public ViewHolder(View view) {
view.setTag(this);
code = (TextView) convertView.findViewById(R.id.code);
name = (CheckBox) convertView.findViewById(R.id.checkBox1);
btnnn = (Button) convertView.findViewById(R.id.btn);
txtv = (TextView) convertView.findViewById(R.id.txt);
name.setOnClickListener(this);
btnnn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v == btnnn) {
// ViewHolder will be reused, you better move counter to checkTable
checkTable.counter++;
txtv.setText(String.valueOf(checkTable.counter));
} else if (v == name) {
country.setSelected(name.isChecked());
}
}
}