【发布时间】:2013-11-30 09:15:44
【问题描述】:
我有一个基础适配器类,我用它来填充列表视图。一些内容是在布局文件中定义的,我还需要根据传递给基本适配器的 int 值动态添加一定数量的图像按钮。
obj 是一个具有 int 值以及位图数组列表的对象;
当我运行此代码时,我得到更多的图像按钮,然后是 obj.value 的值。
likePre_pics 是位图数组列表的名称
有人可以帮忙吗?
public class News_Feed_BaseAdapter extends BaseAdapter{
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LinearLayout linLayout =
(LinearLayout)convertView.findViewById(R.id.like_preview_LinearLayout);
for(int i=0; i< obj.value;i++)
{
ImageButton op= new ImageButton(context);
LayoutParams lpView = new LayoutParams(100, 100);
//op.setImageBitmap(obj.get(position).likePre_pics.get(i));
linLayout.addView(op,lpView);
}
}
}
【问题讨论】:
-
致命异常:主要 java.lang.IndexOutOfBoundsException:索引 4 无效,java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) 处的 java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) 处的大小为 4。 .java:308) 在 com.example.foodshootr.News_Feed_BaseAdapter.getView(News_Feed_BaseAdapter.java:96) 在 android.widget.AbsListView.obtainView(AbsListView.java:2177)
-
Sooo,这是什么?您获得的 ImageButton 数量超出预期,或者您的应用因上述错误而崩溃?
-
如果我把 for(int i=0; i
-
好吧,对于第二种情况(多余的 ImageButtons),您错误地使用了 convertView。此参数是要“回收”的视图。您只是将额外的 ImageButtons 添加到已经设置了一些视图的视图上。
-
不要将 ImageButtons 添加到 convertView,而是从列表项布局 xml 中扩充一个新视图,将它们添加到该视图,然后将该视图作为 getView 返回值返回。
标签: android listview bitmap android-custom-view baseadapter