我正在尝试制作一个具有多个布局的列表视图。布局由数字0-5拾取。所以我有6种可能的布局。我用一个开关盒来填充与数字相对应的布局。我遇到了一些问题。首先,我得到了某些布局的副本,这些布局具有以前布局的值,甚至不应该存在。下面代码的另一个问题是,如果我滚动到底部,最后一个元素的布局为大小写4,那么当我滚动到顶部并返回底部时,它的布局来自不同的大小写。为什么会发生这种情况。此外,如果我尝试使用settext()设置文本视图,我会收到一个错误,说文本视图为null。
@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;
}
清理代码后,很明显,您有一些杂散代码将string
视图(视图的糟糕名称,btw)分配了两次:
holder1.string = (TextView) v.findViewById(R.id.string0);
holder1.string = (TextView) v.findViewById(R.id.string_two0);
其中一个可能是错误的。