您的位置:首页 > 其它

使用ListView的问题记录

2016-06-03 22:33 281 查看
1.使用ListView实现多个布局的加载的时候遇到了问题:首先我们看看我一开始写的方法:

 private class MyAdapter extends BaseAdapter {

protected ArrayList list;

public MyAdapter(ArrayList<?> list) {
this.list = list;
}

@Override
public int getCount() {
return list.size();
}

@Override
public Object getItem(int position) {
return list.get(position);
}

@Override
public long getItemId(int position) {
return list.get(position).hashCode();
}

@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
if(position<10){//这里设置你自己的判断逻辑返回类型信息
return 1;
}else{
return 2;
}
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {

switch (getItemViewType(position)) {
case 1:
  
break;
case 2:
 
break;
}

convertView.setTag(pictureViewHolder);*/
} else {
switch (getItemViewType(position)) {
case 1:
break;
case 2:
 
break;
}
}
switch (getItemViewType(position)) {
case 1:
 
case 2:

break;
}

return convertView;

}
}

然后,我每次运行的时候都会报这个错误:java.lang.ArrayIndexOutOfBoundsException: length=2; index=2 at android.widget.AbsListView$RecycleBin........

接着发现了2和2在什么地方出现了,就是我返回的类型数量是2:还有我返回的其中一个类型代表也是2。然后就导致了异常,具体我也不分析了,原因我想大家都能猜到了,但是我想不到,这个兼容性有点弱鸡啊。所以我把返回类型改为了0,1然后就没有了问题。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: