v.getTag() 返回 null 而不是 ViewHolder
我有一个自定义适配器,它有标题和自定义行。有时我的 v.getTag() 在我存储 ViewHolder 的位置返回 null。它不会一直发生,我不知道它何时以及为何发生。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
//Header
if(items.hasDescription() && 0 == position) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.app_list_header, null);
((TextView) v.findViewById(R.id.app_list_header_description_text)).setText(items.getDescription());
return v;
}
ViewHolder holder;
// Inflate app view.
if (v == null || v.getTag() == null) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(textViewResourceId, null); //TODO: parent instead of null?
holder = new ViewHolder();
holder.title = (TextView) v.findViewById(R.id.title);
holder.company = (TextView) v.findViewById(R.id.company);
holder.priceOrStatus = (TextView) v.findViewById(R.id.price);
holder.rating = (RatingBar) v.findViewById(R.id.rating);
holder.icon = (ImageView) v.findViewById(R.id.icon);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
}
App app;
if(items.hasDescription()) {
app = items.get(position-1);
} else {
app = items.get(position);
}
// TODO: Do we need this?
if (null == app || null == holder) {
Log.d(TAG, "app: " +app +" holder: " +holder);
return v;
}
//TODO: FIX THE XML BEFORE SO WE DO NOT NEED TO TRIM IT.
// And get rid of all these ifs!!
if(holder.title != null) {
holder.title.setText(app.getTitle().trim());
}
有人可以帮我吗?
I have a customized adapter that has a header and customized rows. Sometimes my v.getTag() returns null where I have stored my ViewHolder. It does not happen all the times and I can not figure out when and why it accurs.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
//Header
if(items.hasDescription() && 0 == position) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.app_list_header, null);
((TextView) v.findViewById(R.id.app_list_header_description_text)).setText(items.getDescription());
return v;
}
ViewHolder holder;
// Inflate app view.
if (v == null || v.getTag() == null) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(textViewResourceId, null); //TODO: parent instead of null?
holder = new ViewHolder();
holder.title = (TextView) v.findViewById(R.id.title);
holder.company = (TextView) v.findViewById(R.id.company);
holder.priceOrStatus = (TextView) v.findViewById(R.id.price);
holder.rating = (RatingBar) v.findViewById(R.id.rating);
holder.icon = (ImageView) v.findViewById(R.id.icon);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
}
App app;
if(items.hasDescription()) {
app = items.get(position-1);
} else {
app = items.get(position);
}
// TODO: Do we need this?
if (null == app || null == holder) {
Log.d(TAG, "app: " +app +" holder: " +holder);
return v;
}
//TODO: FIX THE XML BEFORE SO WE DO NOT NEED TO TRIM IT.
// And get rid of all these ifs!!
if(holder.title != null) {
holder.title.setText(app.getTitle().trim());
}
Can anyone help me out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在此处使用自定义
ListAdapter
的标准模式。并非所有视图都会被回收,例如当它们首次创建以填充ListView
时。您可能还想在创建适配器时引用
LayoutInflater
以稍微提高效率,请参阅下面的代码片段本质上,您必须始终为
v.getTag()
做好准备返回 null 并相应地膨胀一个新的View
。You're using the standard pattern for a custom
ListAdapter
here. Not all views will be recycled e.g. when they are first created to fill theListView
.You may also want to take a reference the the
LayoutInflater
when you create the adapter to slightly improve efficiency, see snippet belowEssentially you must always be prepared for
v.getTag()
to return null and inflate a newView
accordingly.