滚动时,ViewHolder和部分的Listview失败(Listview with ViewHolder(s) and sections fails when scrolling)

编程入门 行业动态 更新时间:2024-10-25 21:30:39
滚动时,ViewHolder和部分的Listview失败(Listview with ViewHolder(s) and sections fails when scrolling)

我正在建立一个列表视图。 我正在使用这个帖子的答案,之前问了一个问题,但又被卡住了。 我认为这是一个非常奇怪的错误。

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

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

我为两种不同的视图构建了两个ViewHolders。 发生的错误是itemHolder.name.setText(ld.name);上的NullPointerException itemHolder.name.setText(ld.name); 线。 我没有得到的是,代码适用于前几个托管,但在我开始滚动时失败。 在我正在使用的数据中,名称和提示永远不会为空,只有photoUrl可能是但代码中涵盖了这一点。

任何人都知道为什么这段代码失败了?

Im building an listview with sections. i was using this answer of a post and asked a question before, but am stuck again. I think it is a pretty weird error.

When i start my activity, i can see the list on the screen, just as i want it. But the moment i try to start scrolling the activity crashes. I thought i implemented everything the same way, but apparently im not. My adapter:

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

I build two ViewHolders for the two different kind of views. The error that occurs is NullPointerException on the itemHolder.name.setText(ld.name); line. The thing i don't get is that the code works for the first few entrys but fails when i start scrolling. In the data i'm using, name and tip are never empty, only photoUrl might be but that is covered in the code.

Anyone knows why this piece of code is failing?

最满意答案

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

In the code paths where you create a new viewholder and inflate a new view, you never actually store the viewHolder in the Views tag, so when you scroll and get an exisitng view, view.gettag() returns null, and later when you try and use the ViewHolder you get the Null Pointer Exception. You need to add the calls to setTag().

更多推荐

本文发布于:2023-07-17 04:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1139249.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Listview   ViewHolder   sections   scrolling   fails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!