我试图做一个列表视图有多个布局。布局由0-5选择。所以我有6种可能的布局。我使用开关盒来扩充与编号对应的布局。我遇到一些问题。首先即时获得某些布局的重复,这些布局具有以前布局的值,甚至不应该在那里。下面的代码的另一个问题是如果我滚动到buttom最后一个元素具有case 4的布局,那么当我滚动到顶部并返回到buttom它有一个不同情况下的布局。这是为什么发生。此外,如果我尝试使用settext()设置textview,我得到一个错误,说textview为空。listview布局返回空视图
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder1 = null;
View v = convertView;
q = getItemViewType(getType(type, position));
if (v == null) {
holder1 = new ViewHolder();
switch (q) {
case 0:
v = LayoutInflater.from(context).inflate(R.layout.layout0, parent, false);
holder1.mainpic = (ImageView) v.findViewById(R.id.mainimage0);
holder1.string = (TextView) v.findViewById(R.id.string0);
holder1.string2 = (TextView) v.findViewById(R.id.string_two0);
break;
case 1:
v = LayoutInflater.from(context).inflate(R.layout.layout1, parent, false);
holder1.mainpic = (ImageView) v.findViewById(R.id.mainimage1);
holder1.string = (TextView) v.findViewById(R.id.string1);
break;
case 2:
v = LayoutInflater.from(context).inflate(R.layout.layout2, parent, false);
holder1.mainpic = (ImageView) v.findViewById(R.id.mainimage2);
holder1.string = (TextView) v.findViewById(R.id.string2);
break;
case 3:
v = LayoutInflater.from(context).inflate(R.layout.layout3, parent, false);
holder1.mainpic = (ImageView) v.findViewById(R.id.mainimage3);
holder1.string = (TextView) v.findViewById(R.id.string3);
break;
case 4:
v = LayoutInflater.from(context).inflate(R.layout.layout4, parent, false);
holder1.mainpic = (ImageView) v.findViewById(R.id.mainimage4);
holder1.string = (TextView) v.findViewById(R.id.string4);
break;
default:
v = LayoutInflater.from(context).inflate(R.layout.layout5, parent, false);
holder1.mainpic = (ImageView) v.findViewById(R.id.mainimage5);
holder1.string = (TextView) v.findViewById(R.id.string5);
break;
}
v.setTag(holder1);
} else {
holder1 = (ViewHolder) v.getTag();
}
///holder1.string.settext(" some text") error
return v;
}
@Override
public int getItemViewType(int value) {
int type;
if(value ==0)
{
type=0;
}
else if(value ==1)
{
type=1;
}else if(value ==2)
{
type=2;
}
else if(value ==3)
{
type=3;
}
else if(value ==4)
{
type=4;
}else
{
type=5;
}
return type;
}
您应该修改一些代码。使用全部空格和异常缩进来读取有点困难 – codeMagic 2014-09-29 17:06:59
codeMagic-ok我将其清理了一下 – 2014-09-29 17:12:08
检查以确保getCount()方法返回项目的数量。 – 2014-09-29 17:16:53