【问题标题】:listview layout returns null viewslistview 布局返回空视图
【发布时间】:2014-09-29 17:03:48
【问题描述】:

我正在尝试制作具有多种布局的列表视图。布局由数字 0-5 选择。所以我有 6 种可能的布局。我使用开关盒来膨胀与数字相对应的布局。我有几个问题。首先我得到某些布局的副本,这些布局具有以前布局的值,甚至不应该存在。下面代码的另一个问题是,如果我滚动到底部,最后一个元素的布局为案例 4,那么当我滚动到顶部并返回到底部时,它具有来自不同案例的布局。为什么会这样。此外,如果我尝试使用 settext() 设置 textview,我会收到一条错误消息,提示 textview 为空。

@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-好的我清理了一下
  • 检查以确保 getCount() 方法返回项目数。

标签: android android-layout android-activity android-listview android-adapter


【解决方案1】:

清理您的代码后,很明显您有一些杂散代码分配了您的 string 视图(视图名称很糟糕,顺便说一句):

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

其中一个可能是错误的。

【讨论】:

  • 不,那不是问题。在将代码放到网上之前,我必须稍微更改一下代码。这就是为什么我的名字不好
  • 嗯,我可能还建议发布您的 getItemViewType() 和 getViewTypeCount() 代码。并仔细检查您的所有布局是否确实具有您正在寻找的视图 ID。
  • @kcoppock- 我检查了我的布局以确保所有的 Id 都在那里。我发布了我的 getitemViewType
  • 在您的 switch case 中的每个 holder.string 分配之后添加一些日志记录 - 在 findViewById() 调用之后查看它们是否为空。
  • @kcoppock- 我尝试了一些日志记录“ if(holder.string==null) log.d("","null")" 视图不为空。
猜你喜欢
  • 2012-10-04
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-15
  • 1970-01-01
相关资源
最近更新 更多