问题描述
我有延伸BaseAdapter的自定义列表适配器。
我创建了一个holder类标记我为了看法,避免夸大已经存在的意见。
I have a custom list adapter that extends BaseAdapter.I create a holder class to tag my views in order to avoid inflating views that already exist.
static class ViewHolder{
TextView tvName;
TextView tvDescription;
Button btAdd;
}
在我getView我有财产以后这样的:
and in my getView i have somthing like this:
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
ViewHolder holder = null;
if(vi == null){
vi = inflater.inflate(R.layout.list_row, null);
holder = new ViewHolder();
holder.tvName = (TextView)vi.findViewvById(R.id.name);
holder.tvDescription = (TextView)vi.findViewvById(R.id.desc);
holder.btAdd = (Button)vi.findViewvById(R.id.btnadd);
}else{
holder = (ViewHolder) vi.getTag();
}
holder.btnAdd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
holder.btnAdd.startAnimation(scale);
holder.tvName.setVisibility(View.GONE);
}
});
return vi;
}
但是Eclipse从得到错误holder.btnAdd.startAnimation(规模);并表示持证人应是最终的......
But eclipse get error from "holder.btnAdd.startAnimation(scale);" and says holder should be final......
我怎样才能解决这个问题?
How can I fix it?
推荐答案
您应该获得的视图与
的onClick
方法如下:
holder.btnAdd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(scale);
}
});
您传递视图V
参数在的onClick
方法。然后,你的方法连接到View这是你的情况: holder.btnAdd
,所以 v == holder.btnAdd
You pass the View v
parameter in your onClick
method. Then, your method is attached to the View which is in your case: holder.btnAdd
, therefore v == holder.btnAdd
更新结果
这是一个有点笨我做了什么来解决这个问题。这是很容易的,你的确初始化 viewHolder
为空
,但你不应该。这就是为什么你不能让你的 viewHolder
为最后
:
Update
It's a bit stupid what I did to resolve this. This is really easy, indeed you initialize viewHolder
as null
but you shouldn't. That's why you can't make your viewHolder
as final
:
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
final ViewHolder holder; // without initialized
if(vi == null){
vi = inflater.inflate(R.layout.list_row, null);
holder = new ViewHolder();
holder.tvName = (TextView)vi.findViewvById(R.id.name);
holder.tvDescription = (TextView)vi.findViewvById(R.id.desc);
holder.btAdd = (Button)vi.findViewvById(R.id.btnadd);
}else{
holder = (ViewHolder) vi.getTag();
}
holder.btnAdd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(scale);
holder.tvName.setVisibility(View.GONE);
}
});
return vi;
}
这就是它!
这篇关于设置在onClickListener getview()与ViewHolder类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!