gpt4 book ai didi

android - 滚动时带有 ViewHolder 和部分的 ListView 失败

转载 作者:太空宇宙 更新时间:2023-11-03 13:32:11 25 4
gpt4 key购买 nike

我正在构建一个包含部分的 ListView 。我用的是this一个帖子的回答,之前问过一个问题,但是又卡住了。我认为这是一个非常奇怪的错误。

当我开始我的 Activity 时,我可以在屏幕上看到列表,就像我想要的那样。但是当我尝试开始滚动时, Activity 崩溃了。我以为我以同样的方式实现了一切,但显然我不是。我的适配器:

public class DelftAdapter extends BaseAdapter {


private static final int TYPE_ITEM = 0;
private static final int TYPE_SECTION = 1;
private Activity activity;
private List<ListItem> listItems;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
private final int[] bgColors = new int[] { R.color.list_odd, R.color.list_even };


public DelftAdapter(Activity a, ArrayList<ListItem> li) {
activity = a;
listItems = li;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());

}

public int getCount() {
return listItems.size();
}

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}

@Override
public int getItemViewType(int position) {
return listItems.get(position).isSection() ? TYPE_SECTION : TYPE_ITEM;
}

@Override
public int getViewTypeCount() {
return 2; // sectionheader and regular item
}


public View getView(int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
View vi=convertView;
final ListItem li = listItems.get(position);
ItemViewHolder itemHolder;
SectionViewHolder sectionHolder;

switch (type) {
case TYPE_SECTION: // is sectionheader
if (vi == null) { //convertview==null
sectionHolder = new SectionViewHolder();
vi = inflater.inflate(R.layout.sectionedlistitem, null);
vi.setOnClickListener(null);
vi.setOnLongClickListener(null);
vi.setLongClickable(false);
sectionHolder.title = (TextView) vi.findViewById(R.id.list_header_title);
}else{//convertview is not null
sectionHolder = (SectionViewHolder)vi.getTag();
}
SectionItem si = (SectionItem)li;
sectionHolder.title.setText(si.getTitle());
break;
case TYPE_ITEM:// no sectionheader
if (vi == null) { //convertview==null
itemHolder = new ItemViewHolder();
vi = inflater.inflate(R.layout.singlelistitem, null);
itemHolder.name=(TextView)vi.findViewById(R.id.tvname);
itemHolder.tip=(TextView)vi.findViewById(R.id.tvtip);
itemHolder.image=(ImageView)vi.findViewById(R.id.image);
}else{ // convertview != null
itemHolder = (ItemViewHolder)vi.getTag();
}
ListData ld = (ListData)li;
itemHolder.name.setText(ld.name);
itemHolder.tip.setText(ld.tip);
if (ld.photoUrl != null ){
imageLoader.DisplayImage(ld.photoUrl, itemHolder.image);
}else{
itemHolder.image.setImageURI(Uri.fromFile(new File("//assets/nopic.png")));
}
// alternating colors
int colorPos = position % bgColors.length;
vi.setBackgroundResource(bgColors[colorPos]);
break;
}


return vi;

}
public static class SectionViewHolder {
public TextView title;
}

public static class ItemViewHolder {
public TextView name;
public TextView tip;
public ImageView image;
}

}

我为两种不同类型的 View 构建了两个 ViewHolder。发生的错误是 itemHolder.name.setText(ld.name); 行上的 NullPointerException。我没有得到的是代码适用于前几个条目但在我开始滚动时失败。在我使用的数据中,name 和 tip 永远不会为空,只有 photoUrl 可能是空的,但代码中包含了这一点。

有人知道为什么这段代码会失败吗?

最佳答案

在您创建新 View 持有者和扩展新 View 的代码路径中,您实际上从未将 viewHolder 存储在 Views 标记中,因此当您滚动并获取现有 View 时,view.gettag() 返回 null,稍后当您尝试使用 ViewHolder 时,您会得到 Null Pointer Exception。您需要添加对 setTag() 的调用。

关于android - 滚动时带有 ViewHolder 和部分的 ListView 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11620988/

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