我試圖做一個列表視圖有多個佈局。佈局由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