1

我试图做一个列表视图有多个布局。布局由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; 

}

+1

您应该修改一些代码。使用全部空格和异常缩进来读取有点困难 – codeMagic 2014-09-29 17:06:59

+0

codeMagic-ok我将其清理了一下 – 2014-09-29 17:12:08

+0

检查以确保getCount()方法返回项目的数量。 – 2014-09-29 17:16:53

回答

1

清理你的代码后,很明显你有一些流浪代码(视图可怕的名字,顺便说一句)指定您的string视图两次:

holder1.string = (TextView) v.findViewById(R.id.string0); 
holder1.string = (TextView) v.findViewById(R.id.string_two0); 

之一这些可能是错误的。

+0

不,这不是问题。在我把它放到网上之前,我不得不改变我的代码。这就是为什么我有坏名字 – 2014-09-29 17:28:31

+0

嗯,以及我可能也建议张贴您的代码getItemViewType()和getViewTypeCount()。并仔细检查您的所有布局实际上都有您要查看的视图ID。 – kcoppock 2014-09-29 17:30:04

+0

@ kcoppock-我检查了我的布局以确保所有的ID都在那里。我发布了我的getitemViewType – 2014-09-29 17:38:56